Python:列表推导语句中的语法错误

时间:2012-06-21 14:00:41

标签: python

missing_buckets = [x in timebuckets if ((x not in found_tenors) and (x == '1y' or x[-1] != 'y'))]

我收到错误:

missing_buckets = [x in timebuckets if ((x not in found_tenors) and (x == '1y' or x[-1] != 'y'))]
                                                                                                ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:6)

missing_buckets = [x for x in timebuckets if ((x not in found_tenors) and (x == '1y' or  x[-1] != 'y'))]
                   ^^^^^

答案 1 :(得分:3)

你忘了“for x in”:

missing_buckets = [x for x in timebuckets if ((x not in found_tenors) and (x == '1y' or x[-1] != 'y'))]