简体   繁体   English

如何将不同长度的行放在一个数组中或 python 中的列表中?

[英]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.

相关问题 如何将数组和列表放在一起并根据数组的值过滤列表? - How can I put together an array and a list and filter the list base on the values of the array? 如何将来自 for 循环的各种元素放入列表中? - How can I put the various elements come from for loop in a list? 如何将我的代码放在 Python 中? - How can i put my Code together in Python? 如何在Python中创建包含各种类型元素的列表? - How can I make a list that contains various types of elements in Python? 如何将一个数组的多行和多列分配给 Python 中另一个数组的相同行和列? - How can I assign multiple rows and columns of one array to the same rows and columns of another array in Python? Python - 转置不同长度的列表列表 - 3.3最简单的方法 - Python - Transpose List of Lists of various lengths - 3.3 easiest method 如何将列表的一部分分成多个列表,然后将它们全部放在一个大的嵌套列表中? - How do I seperate parts of a list into multiple lists, and then put them all together into one big nested list? 是我可以将数组函数的总和和平均值组合在一起以创建一个函数的一种方法吗? - Is their a way i can put a sum and average of array functions together to create one function? 如何从文件中读取python中的数组列表并将其放入数组列表中? - How can i read a list of arrays in python from a file and put it in a list of array? 如何从python列表中将具有不同长度的元组的数据写入文件? - How do I write data from python lists with various lengths of tuples inside to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM