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

5ssjco0h  于 2021-09-08  发布在  Java
关注(0)|答案(3)|浏览(401)

这是我的代码:

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
eqoofvh9

eqoofvh91#

你不能使用像这样的字符串 "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}")
2ul0zpep

2ul0zpep2#

使用 slice S你只需要在车站号码上加一个。
您还需要对结果进行循环。你可以用 zip 并行地循环。

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
w6mmgewl

w6mmgewl3#

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

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])

相关问题