Python检查多个列表的变量

时间:2015-07-28 14:55:25

标签: python-3.x

所以我有3个数据列表,我需要测试我从json响应得到的任何数据是否在任何列表中,我可能对它很愚蠢但是我试过学习,似乎无法正常工作。

list1 = ['a', 'b', 'c']
list2 = ['a1', 'b1', 'c1']
list2 = ['a2', 'b2', 'c2']

#block of code...
#block of code...

content = json.loads(response.read().decode('utf8'))
data = content
for x in data:
   #if x['name'] in list1: #This works fine the line below does not.
   if x['name'] in (list1, list2, list3):
   print("something")

3 个答案:

答案 0 :(得分:1)

我建议一些简单明了的事情:

if (x['name'] in list1 or 
    x['name'] in list2 or 
    x['name'] in list3):
    ...

答案 1 :(得分:1)

作为此类任务的pythinc方式,您可以使用any模拟OR操作数,all用于and操作数。

所以她可以在any()中使用generator expression

if any(x['name'] in i for i in (list1, list2, list3))

答案 2 :(得分:0)

连接列表怎么样?

if x['name'] in [list1 + list2 + list3]: