Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

TypeError: str decoding is not supported during iteration on a list ...

$
0
0

I have a list of tuples

a = [('BC', 'CONTAINS'), ('CD', 'FREE_FROM'), ('BM', 'CONTAINS') , ('ZZ', nan), (nan, nan), (nan, 'FREE_FROM'), (nan, nan)]

I want a list which does not contain any nan and also does not contain element in which second element of tuple is 'FREE_FROM' such that the resulting list looks like the following:

res = [('BC', 'CONTAINS'), ('BM', 'CONTAINS')]

Currently I am doing it like this:

res = [(x,y) for x,y in containsAllergen if (str(x,y) != ('nan', 'nan') or str(y) != 'FREE_FROM')]

but it is throwing the error mentioned in the subject:

TypeError: decoding str is not supported

Any suggestions? Thanks.

I think the error is in :

str(x,y)

Beside, I think you will need the "and" operator to produce the result you need:

res = [(x,y) for x,y in a if (str(x) != 'nan' and str(y) != 'nan' and str(y) != 'FREE_FROM')]

Viewing all articles
Browse latest Browse all 9596

Latest Images

Trending Articles



Latest Images