简体   繁体   English

遍历两个不同长度的列表

[英]Iterating through two lists with different lengths

I am trying to iterate through two lists with different lengths and compare the strings inside them, but I keep getting this TypeError "list indices must be integers or slices, not str".我试图遍历具有不同长度的两个列表并比较其中的字符串,但我不断收到此类型错误“列表索引必须是整数或切片,而不是 str”。 Here's my code.这是我的代码。 How can I go about this?我该怎么办?

在此处输入图片说明

See my comment above, there are a bunch of problems with your code.看我上面的评论,你的代码有很多问题。 To get you started, though, please look at this working example and understand what's happening:不过,为了让您开始,请查看这个工作示例并了解发生了什么:

color_list_1 = ["White", "Black", "Red"]
color_list_2 = ["Red", "Green", "Brown"]

for color_1, color_2 in zip(color_list_1, color_list_2):
    if color_1 == color_2:
        print("Equal:", color_1, color_2)

Play around with it, make the lists different lengths and explore.玩弄它,使列表的长度不同并进行探索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM