[英]Python Add Elements to Lists within List if Missing
Given the following list of lists: 鉴于以下列表清单:
a = [[2,3],[1,2,3],[1]]
I need each list within a to have the same number of elements. 我需要a中的每个列表具有相同数量的元素。 First, I need to get the longest length of any list in a.
首先,我需要获得a中任何列表的最长长度。 Then, I need to ensure all lists are at least that long.
然后,我需要确保所有列表至少那么长。 If not, I want to add a zero (0) to the end until that is true.
如果没有,我想在结尾添加零(0),直到这是真的。 The desired result is:
期望的结果是:
b = [[2,3,0],[1,2,3],[1,0,0]]
Thanks in advance! 提前致谢!
PS I also need to apply this to a Pandas Data Frame like this one: PS我还需要将它应用于像这样的Pandas数据框:
import pandas as pd
b = [[2,3,0],[1,2,3],[1,0,0]]
f=pd.DataFrame({'column':b})
First, compute the maximum length of your elements: 首先,计算元素的最大长度:
maxlen=len(max(a,key=len)) # max element using sublist len criterion
or as Patrick suggested do it using generator comprehension on sublist lengths, probably a tad faster: 或者帕特里克建议在子列表长度上使用生成器理解来做它,可能要快一点:
maxlen=max(len(sublist) for sublist in a) # max of all sublist lengths
then create a new list with 0 padding: 然后使用0填充创建一个新列表:
b = [sl+[0]*(maxlen-len(sl)) for sl in a] # list comp for padding
result with a = [[2,3],[1,2,3],[1]]
: 结果
a = [[2,3],[1,2,3],[1]]
:
[[2, 3, 0], [1, 2, 3], [1, 0, 0]]
Note: could be done in one line but would not be very performant because of the recomputation of maxlen. 注意:可以在一行中完成,但由于重新计算maxlen而不会非常高效。 One-liners are not always the best solution.
单行并不总是最好的解决方案。
b = [sl+[0]*(len(max(a,key=len))-len(sl)) for sl in a] # not very performant
How about 怎么样
pd.DataFrame(a).fillna(0)
to get exactly what you asked for 准确地得到你所要求的
pd.Series(pd.DataFrame(a).fillna(0).astype(int).values.tolist()).to_frame('column')
this is also related to this question 这也与这个问题有关
where you can get much better performance with 你可以在哪里获得更好的表现
def box(v):
lens = np.array([len(item) for item in v])
mask = lens[:,None] > np.arange(lens.max())
out = np.full(mask.shape, 0, dtype=int)
out[mask] = np.concatenate(v)
return out
pd.DataFrame(dict(columns=box(a).tolist()))
for i in a:
while len(i) < 3:
i.append(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.