我正在尝试制作一个复杂的测验,其中涉及问题列表和列表中包含的答案。我想使用值使用“for”循环来调用这个多维列表中的数字。
价值观:
items = [[[1, 2], [3, 4], [5, 6]], [[10, 9], [8, 7], [6, 5]]]
这是我尝试过的:
for a, b, c in items:
print(a)
print(b)
print(c)
结果:
[1, 2]
[3, 4]
[5, 6]
[10, 9]
[8, 7]
[6, 5]
我的期望:
类似这样的:
[[[1, 2], [3, 4], [5, 6]], [[10, 9], [8, 7], [6, 5]]]
[[1, 2], [3, 4], [5, 6]]
[1, 2]
如何像这样直接获取值?
您可以像这样手动从列表中获取值:
items
items[value]
items[value][value]
items[value][value][value]
使用 while 循环来代替
items = [[[1, 2], [3, 4], [5, 6]], [[10, 9], [8, 7], [6, 5]]]
# Make items as an element of list and declare it as x
x = [items]
while True:
# break the loop if x[0] is integer
if isinstance(x[0], int): break
print(x[0])
x = x[0] # Now x[0] becomes x
输出:
[[[1, 2], [3, 4], [5, 6]], [[10, 9], [8, 7], [6, 5]]]
[[1, 2], [3, 4], [5, 6]]
[1, 2]