所以我有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")
答案 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]: