【问题标题】:Comparing list and returning index比较列表和返回索引
【发布时间】:2018-03-18 10:54:47
【问题描述】:
List1 = ['3','1','2']
List2 = ['0','1','0']
List3 = ['string1','string2','string3']

我想做

 if List1[i] > 3 and List2[i] = 0 # i = iterating through list1/2 at the same time and comparing them
     print(List3[i]) # i = being the index number found when the if statement is met

#expected output = 'string1'

上下文:https://repl.it/@glasgowm1498/GreenyellowKnowingCommands

【问题讨论】:

标签: python python-3.x list compare


【解决方案1】:

我会使用 python 的内置 zip 函数 documentation

for item1, item2, item3 in zip(list1, list2, list3):
    if item1 == item2:
        print(item3)

希望对你有帮助!

【讨论】:

  • 谢谢!我已经在使用这种方法,但没有意识到你可以将它用于 3 个项目。感谢您实际做出回应,而不是像这里的新手问题中似乎普遍存在的那样抱怨格式化和投反对票
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多