为什么 str() 对过滤器对象不起作用?

问题描述 投票:0回答:1
besthand = "AAAAJ"
besthand = str(filter("J".__ne__, besthand))

为什么besthand的类型仍然是过滤对象?我看到你可以使用

besthand = "".join(besthand)

但我不完全理解为什么 str() 不起作用。是因为过滤器对象是可迭代的吗?

附注我知道它几乎是重复的,但我找不到这个问题的具体答案

python-3.x string filter
1个回答
0
投票

您应该直接传递给

join
,不要将生成器转换为字符串:

besthand = 'AAAAJ'
besthand = ''.join(filter('J'.__ne__, besthand))
© www.soinside.com 2019 - 2024. All rights reserved.