[英]How can I put together rows with various lengths in in one array or list in python?
I have some rows like我有一些像
a=[142, 205, 206, 238, 366]
b=[12,14]
c=[1, 5, 8]
and I want to put them together in a final array or list with three rows that have different lengths.我想将它们放在一个最终的数组或列表中,其中包含三行不同长度的行。 I used append, concatenate and hstack but all of them need the data have the same dimensions.
我使用了 append、连接和 hstack,但它们都需要数据具有相同的维度。 What can I use please?
请问我可以用什么? Thanks
谢谢
I mean I have a loop that I want to stick lots of rows together (in d) and read every element later from d in another loop based on the column and row of d.我的意思是我有一个循环,我想将很多行粘在一起(在 d 中),然后根据 d 的列和行在另一个循环中从 d 读取每个元素。 I have a big for loop in my code but as an example simply instead of a for consider that I want to do this procedure iteratively like below for only four rows (a,b,c,k):
我的代码中有一个大的 for 循环,但作为一个示例,而不是 for 考虑我想迭代地执行此过程,如下所示仅针对四行(a,b,c,k):
d=[]
#below are rows of assumed data
a = [142, 205, 206, 238, 366]
b = [12,14]
c = [1, 5, 8]
k=[] #might have null data as well
d=[d,a]
print(d)
d=[d,b]
print(d)
d=[d,k]
print(d)
d=[d,c]
print(d)
d[1][0]
But finally this is the answer that I get:但最后这是我得到的答案:
[[], [142, 205, 206, 238, 366]]
[[[], [142, 205, 206, 238, 366]], [12, 14]]
[[[[], [142, 205, 206, 238, 366]], [12, 14]], []]
[[[[[], [142, 205, 206, 238, 366]], [12, 14]], []], [1, 5, 8]]
1
which is not right and it should be d[1][0]=142这是不对的,应该是 d[1][0]=142
Numpy does not support jagged arrays, so your best option is a list. Numpy 不支持锯齿状 arrays,因此您最好的选择是列表。
a = [142, 205, 206, 238, 366]
b = [12,14]
c = [1, 5, 8]
d = [a, b, c]
I'm still not entirely sure I understand your edit, but tell me if this example seems like what you're looking for:我仍然不完全确定我理解您的编辑,但请告诉我这个示例是否看起来像您正在寻找的内容:
combined_list = []
print(combined_list)
list_1 = [14, 17, 12, 16, 18, 0, 9, 11, 8, 5]
combined_list.append(list_1)
print(combined_list)
list_2 = [12, 9, 11, 4, 5]
combined_list.append(list_2)
print(combined_list)
list_3 = [2, 14, 7]
combined_list.append(list_3)
print(combined_list)
list_4 = [4, 2, 17, 9, 9, 11, 15, 14, 14]
combined_list.append(list_4)
print(combined_list)
d = [a,b,c] will put them in a list of lists
to make it into an array:使其成为一个数组:
import numpy as np
np.array(d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.