如何使用&x201C;列表”;使用python添加一个新列,查找元素之间的对应关系?;
问题描述如下: 现在我们有了如何使用&x201C;列表”;使用python添加一个新列,查找元素之间的对应关系?;,python,algorithm,Python,Algorithm,问题描述如下: 现在我们有了list1和list2。它们的行数和列数不是固定的,但它们的行数相同,因此我们可以将list2添加到list1作为新列。但是,请注意,我们使用的列表本质上是一维数组,除了列表中的元素是元组。。。它们看起来像二维数组,但实际上不是。它们本质上是一维列表 示例问题: list1= [("A","the first",1,"one"),("B","the second",2,
list1
和list2
。它们的行数和列数不是固定的,但它们的行数相同,因此我们可以将list2
添加到list1
作为新列。但是,请注意,我们使用的列表本质上是一维数组,除了列表中的元素是元组。。。它们看起来像二维数组,但实际上不是。它们本质上是一维列表
示例问题:
list1= [("A","the first",1,"one"),("B","the second",2,"two"),("C","the third",3,"three"),("D","the 4th",4,"four")]
list2 = [("C","cat","animal" ),("B","banana","fruit"),("A","apple","fruit") ,("D","do","verb") ]
样本输出:
[("A","the first",1,"one","apple","fruit"),
("B","the second",2,"two","banana","fruit" ),
("C","the third",3,"three","cat","animal" ),
("D","the 4th",4,"four","do","verb")]
请注意,list1
和list2
中的列数不是固定的,行数必须相等。您可以使用list1
中的列0作为合并lsit1和List2的参考。您需要去掉重复的列
我知道这个简单的算法在任何情况下都可以编写。如何使整个算法简洁、巧妙
from collections import defaultdict
list1= [("A","the first",1,"one"),("B","the second",2,"two"),("C","the third",3,"three"),("D","the 4th",4,"four")]
list2 = [("C","cat","animal" ),("B","banana","fruit"),("A","apple","fruit") ,("D","do","verb") ]
d = defaultdict(lambda: list())
for column, *rest_cols in list1+list2:
d[column] += [c for c in rest_cols if c not in d[column]] # can also made with |= set(rest_cols) and d as defaultdict of set() but the order won't maintained
output = [(k, *v) for k, v in d.items()] # converting back to the required list of tuples
print(output)
给出:
[('A', 'the first', 1, 'one', 'apple', 'fruit'), ('B', 'the second', 2, 'two', 'banana', 'fruit'), ('C', 'the third', 3, 'three', 'cat', 'animal'), ('D', 'the 4th', 4, 'four', 'do', 'verb')]
给出:
[('A', 'the first', 1, 'one', 'apple', 'fruit'), ('B', 'the second', 2, 'two', 'banana', 'fruit'), ('C', 'the third', 3, 'three', 'cat', 'animal'), ('D', 'the 4th', 4, 'four', 'do', 'verb')]
这是一个简单的zip()应用程序,添加了一个在索引级别不匹配列表的扭曲:
list1 = [("A","the first",1,"one"),("B","the second",2,"two"),("C","the third",3,"three"),("D","the 4th",4,"four")]
list2 = [("C","cat","animal" ),("B","banana","fruit"),("A","apple","fruit") ,("D","do","verb") ]
result = [ a+b[1:] for a,b in zip(sorted(list1),sorted(list2)) ]
for row in result:print(row)
('A', 'the first', 1, 'one', 'apple', 'fruit')
('B', 'the second', 2, 'two', 'banana', 'fruit')
('C', 'the third', 3, 'three', 'cat', 'animal')
('D', 'the 4th', 4, 'four', 'do', 'verb')
但是,对于这种“键控”结构,我建议使用字典而不是元组列表。这是一个简单的zip()应用程序,添加了一个扭曲,列表在索引级别上没有顺序匹配:
list1 = [("A","the first",1,"one"),("B","the second",2,"two"),("C","the third",3,"three"),("D","the 4th",4,"four")]
list2 = [("C","cat","animal" ),("B","banana","fruit"),("A","apple","fruit") ,("D","do","verb") ]
result = [ a+b[1:] for a,b in zip(sorted(list1),sorted(list2)) ]
for row in result:print(row)
('A', 'the first', 1, 'one', 'apple', 'fruit')
('B', 'the second', 2, 'two', 'banana', 'fruit')
('C', 'the third', 3, 'three', 'cat', 'animal')
('D', 'the 4th', 4, 'four', 'do', 'verb')
然而,对于这种“键控”结构,我建议使用字典而不是元组列表