[英]Iterate over list to get values in a specified sequence
I had an initial tuple, over which I was trying to iterate to perform further calculations.我有一个初始元组,我试图对其进行迭代以执行进一步的计算。 However, I end up having an error " too many values to unpack
" which was solved by following suggestions from this question asked previously( Python - too many values to unpack ).但是,我最终遇到了一个错误“ too many values to unpack
”,这是通过遵循之前提出的这个问题的建议解决的( Python - 解压的值太多)。 However, right now after converting it to a list, I have another error 'list' object has no attribute 'reshape'
.但是,现在将其转换为列表后,我有另一个错误'list' object has no attribute 'reshape'
。
My code looks like this:我的代码如下所示:
Z=[([A,3],[A1,6])] #A and A1 are 2x2 matrices
y=[]
for data,label in Z:
x = data.reshape((4,))
y.append(int(label))
I can understand the cause of the error.我可以理解错误的原因。 In this case, I want the loop to run two times (because I have two sets of data [A-3,A1-6]) and data, label
to be: A
, 3
and A1
, 6
.在这种情况下,我希望循环运行两次(因为我有两组数据 [A-3,A1-6])和data, label
为: A
, 3
和A1
, 6
。 But it is being misread as data = [A,3]
and label = [A1,6]
.但它被误读为data = [A,3]
和label = [A1,6]
。
What would be the proper way to loop through this list and generate data the way I want?循环遍历此列表并以我想要的方式生成数据的正确方法是什么?
NB: The Z
doesn't need to be a list, I can change it to a tuple if that makes life easier.注意: Z
不需要是一个列表,如果这让生活更轻松,我可以将其更改为元组。
The tuple is inside a list.元组在列表中。 You're just iterating over the list, but not over the tuple elements.您只是遍历列表,而不是遍历元组元素。 You need nested loops.您需要嵌套循环。
for t in Z:
for data, label in t:
x = data.reshape(4,)
y.append(int(label))
You don't need the nested loops if you get rid of the list.如果您摆脱了列表,则不需要嵌套循环。
Z=([A,3],[A1,6]) #A and A1 are 2x2 matrices
y=[]
for data,label in Z:
x = data.reshape((4,))
y.append(int(label))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.