这是我的代码:
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
不能使用像
"0, 3"
这样的字符串作为列表中的下标。您应该将元组或列表放入numbs
您还需要循环这些元组指定的索引范围。单个
print()
调用不会自动循环并打印多行您可以使用
zip()
在多个列表上循环,并使用切片在列表范围上循环使用^{} s。你只需要在车站号码上加一个
您还需要对结果进行循环。您可以使用^{} 并行循环
输出:
我想你们把列表的概念和字符串混淆了
变量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
我认为您需要同时选择每个列表的不同长度。 比如你右手数一,另一只手数一到五
所以我建议您使用下面的代码
相关问题 更多 >
编程相关推荐