Python按索引比较2个列表

时间:2017-09-24 08:13:49

标签: python-2.7 python-3.x list loops for-loop

如何通过索引迭代两个列表比较值。我已尝试过循环和拉链的使用。

 for a,b in zip(list1,list2):
    if a[0] in b[4]
       print ('found')

修改

这就是我之后

  results = cHandlers.fetchall() #from an sql query
  response = (r.json()) # from a json request
  for u in range(0,3):
     for row in results:
        if (response['data'][u]['item']) == row[3]
           print (found)     

1 个答案:

答案 0 :(得分:1)

zip生成一个元组(a,b)列表,其中a是来自list1的{​​{1}}和来自b的{​​{1}}。要检查所有元素,您可以执行以下操作

list2

要检查具体的指数,您可以使用:

list1 = [1,2,3,5,4]
list2 = [5,3,4,3,4]

for a in zip(list1,list2):
    if a[0] == a[1]:
        print ('found')

同样,在zipped = zip(list1,list2) if zipped[0][0] == zipped[4][1]: print ('found') 元组中,元素0对应zipped,元素1对应list1

相关问题