如何基于枚举数合并两个列表?

时间:2019-04-22 21:10:30

标签: python

如何合并两个枚举列表,如果第一个数字相同,则将“列表1”中的元素更改为“列表2”中的元素?

清单1:

[(1, 'a’), (2, ['00:00:00,280', '00:00:09,680\n']), (3, 'b), (4, 'c'), (5, 'd’),
 (6, ['00:00:08,760', '00:00:13,309\n']), (7, 'e'), (8, 'f'), (9, 'g'),
 (10, ['00:00:09,680', '00:00:15,630\n'])]

清单2:

[(2, ['00:00:00,280', '00:00:08,760']), (6, ['00:00:08,760', '00:00:09,680']),
 (10, ['00:00:09,680', '00:00:13,309'])]

结果:

[(1, 'a’), (2, ['00:00:00,280', '00:00:08,760']), (3, 'b), (4, 'c'), (5, 'd’),
 (6, ['00:00:08,760', '00:00:09,680']),  (7, 'e'), (8, 'f'), (9, 'g'),
 (10, ['00:00:09,680', '00:00:13,309'])]

4 个答案:

答案 0 :(得分:2)

将列表转换为字典。

使用第二个dict更新第一个dict。

将第一个列表转换回列表。

排序列表。

a = [(1, 'a'), (2, ['00:00:00,280', '00:00:09,680\n']), (3, 'b'), (4, 'c'), (5, 'd'), (6, ['00:00:08,760', '00:00:13,309\n']), (7, 'e'), (8, 'f'), (9, 'g'), (10, ['00:00:09,680', '00:00:15,630\n'])]
b = [(2, ['00:00:00,280', '00:00:08,760']), (6, ['00:00:08,760', '00:00:09,680']), (10, ['00:00:09,680', '00:00:13,309'])]
c = [(1, 'a'), (2, ['00:00:00,280', '00:00:08,760']), (3, 'b'), (4, 'c'), (5, 'd'), (6, ['00:00:08,760', '00:00:09,680']),  (7, 'e'), (8, 'f'), (9, 'g'), (10, ['00:00:09,680', '00:00:13,309'])]

da = {k:v for k,v in a}
db = {k:v for k,v in b}
da.update(db)

x = sorted((k,v) for k,v in da.items())

print(x == c) # True

编辑:正如@jferard在评论中指出的,我可以写得更简单

da = dict(a)
db = dict(b)
da.update(db)

x = sorted(da.items())

print(x == c) # True

x = sorted({k:v for k,v in a+b}.items())

print(x == c) # True

甚至

x = sorted(dict(a+b).items())

print(x == c) # True

答案 1 :(得分:2)

我的解决方案与这里的其他答案略有不同,但是我认为它具有所需的功能,并且字典转换少了一个。

我将第二个列表更改为字典,然后进行了列表理解以创建新列表。我将第一个条目保留在元组中,并做了一个dict.get()来尝试从第二个列表中获取一个值:

dict_b = {i: j for i,j in b}
new_a = [(i, dict_b.get(i, j)) for i, j in a]

您也不必重新排序列表。这样会导致运行时间稍长一些:

def func_1():
    dict_b = {i: j for i,j in b}
    return [(i, dict_b.get(i, j)) for i, j in a]

%timeit func_1()

2.48 µs ± 506 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)


def func_2():
    da = {k:v for k,v in a}
    db = {k:v for k,v in b}
    da.update(db)

    return sorted((k,v) for k,v in da.items())

%timeit func_2()

3.07 µs ± 71.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

答案 2 :(得分:2)

另一种解决方案:

>>> a = [(1, 'a'), (2, ['00:00:00,280', '00:00:09,680\n']), (3, 'b'), (4, 'c'), (5, 'd'), (6, ['00:00:08,760', '00:00:13,309\n']), (7, 'e'), (8, 'f'), (9, 'g'), (10, ['00:00:09,680', '00:00:15,630\n'])]
>>> b = [(2, ['00:00:00,280', '00:00:08,760']), (6, ['00:00:08,760', '00:00:09,680']), (10, ['00:00:09,680', '00:00:13,309'])]
>>> seen = set()
>>> sorted(v for v in b+a if not (v[0] in seen or seen.add(v[0])))
[(1, 'a'), (2, ['00:00:00,280', '00:00:08,760']), (3, 'b'), (4, 'c'), (5, 'd'), (6, ['00:00:08,760', '00:00:09,680']), (7, 'e'), (8, 'f'), (9, 'g'), (10, ['00:00:09,680', '00:00:13,309'])]

我们在ba)的元素之前考虑for v in b+a的元素,并使用技巧来按幻影顺序获得唯一元素:如果v[0] in seen,我们产生v,否则我们将v[0]添加到seenseen.add(v[0])会产生副作用,并且总是被评估为False)。

如果您对结果顺序不感兴趣:

>>> seen = set()
>>> [v for v in b+a if not (v[0] in seen or seen.add(v[0]))]
[(2, ['00:00:00,280', '00:00:08,760']), (6, ['00:00:08,760', '00:00:09,680']), (10, ['00:00:09,680', '00:00:13,309']), (1, 'a'), (3, 'b'), (4, 'c'), (5, 'd'), (7, 'e'), (8, 'f'), (9, 'g')]

答案 3 :(得分:0)

尝试将key关键字与sorted()一起使用。

list = list1 + list2
print (sorted([list], key=lambda x: x[0]))

键应该是一个标识如何从数据结构中检索可比较元素的函数。在您的情况下,它是元组的第一个元素,因此我们访问[0]。