[英]How do I access the element of a 2D array in the following scenario?
If I have a 2d array 如果我有一个2d阵列
mylist=[[0],[0],[1],[0]]
for idx in range(len(mylist)):
element=mylist[idx]
element=list([0])
for the first element and second element, element=list([1])
for the third element and so on in the same pattern element=list([0])
表示第一个元素和第二个元素, element=list([1])
表示第三个元素,依此类推相同的模式
For example, how do I make element=0
instead of element=list([0])
in the first instance 例如,如何在第一个实例中使element=0
而不是element=list([0])
It is not very clear from your question what you are trying to achieve so I am going to outline several scenarios for you. 从你的问题中不清楚你想要实现什么,所以我将为你概述几个场景。
mylist=[[0],[0],[1],[0]]
for i in range(len(mylist)):
for j in range(len(mylist[i])):
print(mylist[i][j])
Did you choose the right data structure for your problem? 您是否为问题选择了正确的数据结构? It might be worth keeping the items in 1D list. 将这些项目保存在一维清单中可能是值得的。
mylist=[0, 0, 1, 0]
Or alternatively list of tuples, access would be the same as option 1: 或者,元组列表,访问将与选项1相同:
mylist = [(0, 0, 1, 0), (1, 1, 1, 1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.