通过添加nan使所有子列表的长度相同

时间:2018-11-20 19:16:11

标签: python

如何通过在每个子列表上添加np.nan来使所有子列表具有相同的长度(最长的子列表的长度)?

import random
[list(range(0,random.randint(1,5))) for x in range(n)]

因此,如果输出为:

  

[[0,1],[0],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3]]

它应该像这样:

  

[[nan,nan,nan,0,1],[nan,nan,nan,nan,0],[0,1,2,3,4],[0,1,2,   3,4],[nan,0,1,2,3]]

4 个答案:

答案 0 :(得分:3)

import random
import numpy as np

n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
    while len(c) < n:
        c.insert(0, np.nan)
print(a)
  
    
      

[[nan,nan,nan,nan,0],[nan,nan,nan,nan,0],[nan,nan,nan,0,1],[nan,nan,0,1,2 ],[nan,nan,nan,0,1]]

    
  

这是我认为您正在寻找的示例。

答案 1 :(得分:3)

首先,使用max()查找最长子列表的长度。然后,对于每个子列表,使用分片分配,以正确数量的NaN后跟原始列表替换该列表的内容。

import random, math

n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]

# get the maximum length
maxlen = len(max(lists, key=len))

# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
    sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist

答案 2 :(得分:0)

不是大多数pythonic代码,但我认为它可以解决问题:

longest_list_length = 0
for sublist in output:
  if len(sublist) > longest_list_length:
    longest_list_length = len(sublist)

for sublist in output:
  nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
  sublist = nans_to_prepend + sublist

答案 3 :(得分:0)

如果您关心的只是在随机创建过程中生成np.nan填充,则此简单的衬纸就足够了:

[[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

# [[nan, 0, 1, 2, 3], 
#  [nan, nan, 0, 1, 2], 
#  [nan, nan, nan, 0, 1], 
#  [nan, 0, 1, 2, 3],
#  [0, 1, 2, 3, 4]]

您当然可以将5替换为n并确定最大长度。