将不完整的嵌套列表放在矩形ndarray中 [英] Putting incomplete nested lists in a rectangular ndarray
问题描述
在Python(也使用numpy)中,我有一个列表列表列表,每个列表的长度都不同.
In Python (also using numpy) I have a list of lists of lists, with each list being different lengths.
[
[
["header1","header2"],
["---"],
[],
["item1","value1"]
],
[
["header1","header2","header3"],
["item2","value2"],
["item3","value3","value4","value5"]
]
]
我想使此数据结构为矩形:即,确保len(list[x])
对于所有x
是恒定的,len(list[x][y])
对于所有x,y等都是恒定的.
I want to make this data structure rectangular: i.e. guarantee that len(list[x])
is constant for all x
, len(list[x][y])
is constant for all x,y, etc.
(这是因为我要将数据结构导入numpy)
(This is because I want to import the data structure into numpy)
我可以想到做这种事情的各种非Python方法(迭代结构,记录每个级别的最大长度,具有None
的第二遍和填充值,但是必须有更好的方法.
I can think of various unpythonic ways of doing such a thing (iterate over structure, record maximum length at each level, have second pass and pad values with None
, but there must be a better way.
(我也希望解决方案不依赖于结构的维数;即,它也应适用于此类结构的列表...)
(I also would like the solution to not be dependant on the dimensionality of the structure; i.e. it should work on lists of such structures, too...)
有没有一种简单的方法可以使我丢失?
Is there a simple way of doing this that I'm missing?
推荐答案
您可以创建具有所需尺寸的ndarray
并轻松阅读列表.由于列表不完整,因此必须捕获IndexError
,可以在try / exception
块中完成.
You can create a ndarray
with the desired dimensions and readily read your list. Since your list is incomplete you must catch the IndexError
, which can be done in a try / exception
block.
使用numpy.ndenumerate
使解决方案可以轻松扩展到更大的维度(在下面的for循环中添加更多索引i,j,k,l,m,n,...
):
Using numpy.ndenumerate
allows the solution to be easily extensible to more dimensions (adding more indexes i,j,k,l,m,n,...
in the for loop below):
import numpy as np
test = [ [ ["header1","header2"],
["---"],
[],
["item1","value1"] ],
[ ["header1","header2","header3"],
["item2","value2"],
["item3","value3","value4","value5"] ] ]
collector = np.empty((2,4,4),dtype='|S20')
for (i,j,k), v in np.ndenumerate( collector ):
try:
collector[i,j,k] = test[i][j][k]
except IndexError:
collector[i,j,k] = ''
print collector
#array([[['header1', 'header2', '', ''],
# ['---', '', '', ''],
# ['', '', '', ''],
# ['item1', 'value1', '', '']],
# [['header1', 'header2', 'header3', ''],
# ['item2', 'value2', '', ''],
# ['item3', 'value3', 'value4', 'value5'],
# ['', '', '', '']]], dtype='|S10')
这篇关于将不完整的嵌套列表放在矩形ndarray中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!