Why does it say this--> TypeError: 'bool

2019-03-07 04:17发布

问题:

Content is just a text file

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]

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?

回答1:

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.

Try using without the any() function to check if x2 is a number -

if x2.isdigit()

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() function is used on a iterable (lists or generator expression, etc) , to check if any of them is True.