Python|根据第一个元素合并两个列表列表
给定两个长度相等的列表,编写一个Python程序,根据每个子列表的第一个公共元素合并给定的两个列表。
例子:
Input : lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
Output : [[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]
Input : lst1 = [ ['c', 'class'], ['g', 'greek'], ]
lst2 = [['c', 'coder'], ['g', 'god'], ]
Output : [['c', 'class', 'coder'], ['g', 'greek', 'god']]
方法 #1: Python zip()与列表理解
# Python3 program to Merge two list of
# lists according to first element
def merge(lst1, lst2):
return [a + [b[1]] for (a, b) in zip(lst1, lst2)]
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))
输出:
[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]
方法 #2: Python enumerate()与列表理解
# Python3 program to Merge two list of
# lists according to first element
import collections
def merge(lst1, lst2):
return [(sub + [lst2[i][-1]]) for i, sub in enumerate(lst1)]
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))
输出:
[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]
方法 #3: Python字典
在此方法中,我们使用collections.defaultdict初始化 'dict1' 并遍历 'lst1'+'lst2' 并将 'lst1' 的第一个元素作为键,并将两个子列表的元组第二个元素作为值。最后,我们遍历“dict1”并使用所需的输出初始化“dictlist”。
# Python3 program to Merge two list of
# lists according to first element
import collections
def merge(lst1, lst2):
dict1 = collections.defaultdict(list)
for e in lst1 + lst2:
dict1[e[0]].append(e[1])
dictlist = list()
for key, value in dict1.items():
dictlist.append([key]+value)
return dictlist
# Driver code
lst1 = [[1, 'Alice'], [2, 'Bob'], [3, 'Cara']]
lst2 = [[1, 'Delhi'], [2, 'Mumbai'], [3, 'Chennai']]
print(merge(lst1, lst2))
输出:
[[1, 'Alice', 'Delhi'], [2, 'Bob', 'Mumbai'], [3, 'Cara', 'Chennai']]