问题描述
内容只是一个文本文件
tokens = content.split()
topics = [e for (n, x) in enumerate(tokens) for (n2, x2) in enumerate(tokens) for (i, e) in enumerate(tokens) if any(x2.isdigit()) if '.' in x if re.findall('\D+', x) if n < i < n2]
我不明白我如何遍历bool
,还有没有更简洁,更快捷的方式来进行列表理解?
I dont understand how I am iterating through a bool
and also is there a more concise and faster way of doing this list comprehension?
推荐答案
您的问题来自-any(x2.isdigit())
,我猜x2
是一个字符串,所以x2.isdigit()
返回一个bool
,您不能使用any()
功能.
Your issue comes from - any(x2.isdigit())
, I am guessing x2
is a string, so x2.isdigit()
returns a bool
, you cannot use any()
function on it.
尝试不使用any()
函数来检查x2是否为数字-
Try using without the any()
function to check if x2 is a number -
if x2.isdigit()
如果要检查的是x2
中是否包含数字,可以尝试-
If what you want to check is whether x2
has a digit in it or not, you can try -
if any(i.isdigit() for i in x2)
尽管我不知道您要做什么,所以无法检查其他逻辑是否正确.
Though I do not know what you are trying to do, so cannot check if the other logic is good or not.
any()
函数(列表或生成器表达式等),以检查其中是否为True.
any()
function is used on a iterable (lists or generator expression, etc) , to check if any of them is True.
这篇关于为什么这样说-> TypeError:"bool"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!