Python-填补列表中的空白

时间:2019-02-16 16:05:34

标签: python list

我有一个由数据库查询生成的元组列表,例如:

list = [(0,1,1), (1,2,1), (2,4,3), (4,2,1)]

每个元组中的第一个数字必须是一个连续的数字,从0到15。可能还会有缺失的数字,我正在寻找填补空白的最佳方法。

目前,我是通过循环执行此操作的,但是作为Python菜鸟,我认为它很草率,并且有更好的方法:

# first fill in gaps
cnt = 0
for a,b,c in list:
    if a > cnt:
        list.insert(cnt, tuple((cnt, 0, 0)))
    cnt += 1

# then add any missing at end
while cnt < 16:
    list.append(tuple((cnt, 0, 0)))
    cnt += 1

因此,列表开始时的预期输出为:

list = [(0,1,1), (1,2,1), (2,4,3), (3,0,0), (4,2,1), (5,0,0), (6,0,0), (7,0,0), (8,0,0), (9,0,0), (10,0,0), (11,0,0), (12,0,0), (13,0,0), (14,0,0), (15,0,0)]

3 个答案:

答案 0 :(得分:3)

有很多方法,您可以像这样生成新列表:

data = [(0,1,1), (1,2,1), (2,4,3), (4,2,1)]

out = []
for i in range(16):
    if data and i == data[0][0]:
        out.append(data.pop(0))
    else:
        out.append((i, 0, 0))

print(out)
# [(0, 1, 1), (1, 2, 1), (2, 4, 3), (3, 0, 0), (4, 2, 1), 
# (5, 0, 0), (6, 0, 0), (7, 0, 0), (8, 0, 0), (9, 0, 0), 
# (10, 0, 0), (11, 0, 0), (12, 0, 0), (13, 0, 0), (14, 0, 0), (15, 0, 0)]

请注意,我将您的列表重命名为data,因为最好避免将内置函数的名称用作变量。

答案 1 :(得分:2)

您可以将元组列表转换为首先由元组的第一项索引的字典,以便您可以在0到15的范围内进行迭代以找到缺失的索引,并在列表理解中为其生成默认值:

l = [(0,1,1), (1,2,1), (2,4,3), (4,2,1)]
d = {k: v for k, *v in l}
print([(i, *d.get(i, (0, 0))) for i in range(16)])

这将输出:

[(0, 1, 1), (1, 2, 1), (2, 4, 3), (3, 0, 0), (4, 2, 1), (5, 0, 0), (6, 0, 0), (7, 0, 0), (8, 0, 0), (9, 0, 0), (10, 0, 0), (11, 0, 0), (12, 0, 0), (13, 0, 0), (14, 0, 0), (15, 0, 0)]

答案 2 :(得分:0)

为了介绍一些有趣的语言功能(可以用来解决此问题),这里是一个基于Python稳定排序的解决方案,该解决方案具有简单的排序键功能,并且itertools.groupby()用于根据索引对项目进行分组。 / p>

如果输入数据中有此索引,则取两个中的第一个。如果没有该索引,则为默认项。

import itertools

received_items = [(0,1,1), (1,2,1), (2,4,3), (4,2,1)]
print(received_items)

default_items = [(i, 0, 0) for i in range(16)]

# append default items at the end
data = received_items + default_items
# perform stable sort on the index (first element) selected via key function
keyfunc = lambda x: x[0]
data.sort(key=keyfunc)
# always take first item for each index group
out = [next(v) for k,v in itertools.groupby(data, key=keyfunc)]

print(out)

输出为:

[(0, 1, 1), (1, 2, 1), (2, 4, 3), (4, 2, 1)]
[(0, 1, 1), (1, 2, 1), (2, 4, 3), (3, 0, 0), (4, 2, 1), (5, 0, 0), (6, 0, 0), (7, 0, 0), (8, 0, 0), (9, 0, 0), (10, 0, 0), (11, 0, 0), (12, 0, 0), (13, 0, 0), (14, 0, 0), (15, 0, 0)]

供参考: