如何在for循环中调用列表的不同部分?

2024-09-30 00:41:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

list0 = ["1 - Example", "2- Example"]
list1 = ["link1.com", "link2.com"]
list2 = ["Description1", "Description2"]
list3 = ["1.1 - Example", "1.2 - Example", "1.3 - Example", "1.4 - Example", "2.1 - Example", "2.2 - Example", "2.3 - Example"]
list4 = ["link3.com", "link4.com", "link5.com", "link6.com", "link7.com", "link8.com", "link9.com"]
list5 = ["Description3", "Description4", "Description5", "Description6", "Description7", "Description8", "Description9"]
numbs = ["0, 3", "4, 6"]
numb = 0
for x in list1:
    print(list0[numb],",", list1[numb], ",", list2[numb])
    print(list3[numbs[numb]], list4[numbs[numb]], list5[numbs[numb]])
    numb += 1

这是我想要的输出:

1 - Example, link1.com, Description1
1.1 - Example, link3.com, Description3
1.2 - Example, link4.com, Description4
1.3 - Example, link5.com, Description5
1.4 - Example, link6.com, Description6
2 - Example, link2.com, Description2
2.1 - Example, link7.com, Description7
2.2 - Example, link8.com, Description8
2.3 - Example, link9.com, Description9

但是,我遇到了以下错误:

TypeError: list indices must be integers or slices, not str

Tags: comexamplelist2list1list3link1link2list0
3条回答

不能使用像"0, 3"这样的字符串作为列表中的下标。您应该将元组或列表放入numbs

您还需要循环这些元组指定的索引范围。单个print()调用不会自动循环并打印多行

您可以使用zip()在多个列表上循环,并使用切片在列表范围上循环

list0 = ["1 - Example", "2- Example"]
list1 = ["link1.com", "link2.com"]
list2 = ["Description1", "Description2"]
list3 = ["1.1 - Example", "1.2 - Example", "1.3 - Example", "1.4 - Example", "2.1 - Example", "2.2 - Example", "2.3 - Example"]
list4 = ["link3.com", "link4.com", "link5.com", "link6.com", "link7.com", "link8.com", "link9.com"]
list5 = ["Description3", "Description4", "Description5", "Description6", "Description7", "Description8", "Description9"]
numbs = [(0, 3), (4, 6)]

for name, url, description, (start, end) in zip(list0, list1, list2, numbs):
    print(f"{name}, {url}, {description}")
    for subname, suburl, subdesc in zip(list3[start:end+1], list4[start:end+1], list5[start:end+1]):
        print(f"{subname}, {suburl}, {subdesc}")

使用^{}s。你只需要在车站号码上加一个

您还需要对结果进行循环。您可以使用^{}并行循环

slices = [slice(0, 4), slice(4, 7)]
for x0, x1, x2, s in zip(list0, list1, list2, slices):
    print(x0, x1, x2, sep=', ')
    for x3, x4, x5 in zip(list3[s], list4[s], list5[s]):
        print(x3, x4, x5, sep=', ')

输出:

1 - Example, link1.com, Description1
1.1 - Example, link3.com, Description3
1.2 - Example, link4.com, Description4
1.3 - Example, link5.com, Description5
1.4 - Example, link6.com, Description6
2- Example, link2.com, Description2
2.1 - Example, link7.com, Description7
2.2 - Example, link8.com, Description8
2.3 - Example, link9.com, Description9

我想你们把列表的概念和字符串混淆了

numbs = ["0, 3", "4, 6"]

变量numbs是包含数据“0,3”、“4,6”的列表,也就是“0,3”和“4,6”本身就是数据

在“0,3”中不包含0,3,在“4,6”中不包含4,6

如果你想让你的numbs列表包含0,3,4,6个元素,分成两部分(list,tuple,dictionary),你应该用下面类似于double list,tuple,dictionary的代码设置你的变量numbs

numbs = [(0,3),(4,6)]
numbs = [{0,3},{4,6}]
numbs = [[0,3],[4,6]]

我认为您需要同时选择每个列表的不同长度。 比如你右手数一,另一只手数一到五

所以我建议您使用下面的代码

 list0 = ["1 - Example", "2- Example"]
 list1 = ["link1.com", "link2.com"]
 list2 = ["Description1", "Description2"]
 list3 = ["1.1 - Example", "1.2 - Example", "1.3 - Example", "1.4 - Example", "2.1 - Example", "2.2 - Example", "2.3 - Example"]
 list4 = ["link3.com", "link4.com", "link5.com", "link6.com", "link7.com", "link8.com", "link9.com"]
 list5 = ["Description3", "Description4", "Description5", "Description6", "Description7", "Description8", "Description9"]
    
 for i in range(2):
   print(list0[i], list1[i], list2[i])
   if i == 0: 
     for i in range(4): 
       print(list3[i], list4[i], list5[i])
    elif i == 1:
      for i in range(4,7):
        print(list3[i], list4[i], list5[i])

相关问题 更多 >

    热门问题