从稀疏数据创建python列表理解

时间:2018-06-21 17:07:08

标签: python list-comprehension sparse-matrix

我有两个列表(键和值),它们定义了一个稀疏定义的列表,每个键位置都有值。我想将其转换为在每个位置都有值的密集列表。我可以通过如下的for循环来做到这一点。

keys = [1,3,5]
values = [1.0, 3.0, 5.0]
column = [None]*10
for i, k in enumerate(keys):
    column[k] = values[i]

column
>>> [None, 1.0, None, 3.0, None, 5.0, None, None, None, None]

是否可以使用列表理解来创建列?

3 个答案:

答案 0 :(得分:0)

只需使用这种单线"June18th"

list comprehension

输出:

column =[None if i not in dict(zip(keys,values)).keys() else dict(zip(keys,values))[i] for i in range(10)]

答案 1 :(得分:0)

keys = [1,3,5]
values = [1.0, 3.0, 5.0]

column = [values[keys.index(i)] if i in keys else None for i in range(10)]

print(column)

输出

[None, 1.0, None, 3.0, None, 5.0, None, None, None, None]

答案 2 :(得分:0)

我接受了zimdero的回答,因为它肯定回答了这个问题。但是,关于大型列表的时间安排存在疑问。我使用下面的代码为每种情况计时,列表大小为10K,密度为.4(其中60%的值为None)。

对于这种特殊情况,for循环比列表理解快2个数量级,比使用zip的列表理解快3个数量级。查看下面的结果。

import time
import random

# setup test case
iterations = 10
sz = 10000
cutoff = sz*.60
a = random.sample(range(1,sz+1), sz)
dense = [x if x > cutoff else None for x in a]
keys = [i for i, n in enumerate(dense) if n]
values = [x for x in dense if x]

# case 1 for loop
start_time = time.time()
for i in range(iterations):
    column = [None]*sz
    for i, k in enumerate(keys):
        column[k] = values[i]

end_time = time.time()
print("For loop time            :", end_time - start_time)


# case 2 list comprehension
start_time = time.time()
for i in range(iterations):
    column = [values[keys.index(i)] if i in keys else None for i in range(sz)]
end_time = time.time()

print("List comprehension time 1:", end_time - start_time)


# case 2 list comprehension
start_time = time.time()
for i in range(iterations):
    column =[None if i not in dict(zip(keys,values)).keys() else 
dict(zip(keys,values))[i] for i in range(sz)]
end_time = time.time()

print("List comprehension time 2:", end_time - start_time)



For loop time            : 0.00599980354309082 
List comprehension time 1: 6.379000186920166 
List comprehension time 2: 36.09299993515015