[英]How to use “list” of python to add a new column with finding the corresponding relationship between the elements ?
The description of the problem is:问题描述为:
Now we have list1
and list2
.现在我们有了
list1
和list2
。 Their number of rows and columns is not fixed, but they have the same number of rows,So we can add list2
to list1
as new columns.它们的行数和列数不是固定的,但是它们的行数是一样的,所以我们可以将
list2
添加到list1
中作为新的列。 Note, however, that the list we use is essentially a one-dimensional array, except that the elements in the list are tuple... They look like two-dimensional arrays, but they don't.They are essentially one-dimensional lists.但是请注意,我们使用的列表本质上是一个一维数组,只是列表中的元素是元组......它们看起来像二维 arrays,但它们不是。它们本质上是一维列表.
Sample questions:样题:
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") ]
Sample output:样品 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")]
Note that the number of columns in list1
and list2
is not fixed, and the number of rows must be equal.注意
list1
和list2
的列数不是固定的,行数必须相等。 You can use column 0 in list1
as a reference to merge lsit1 and List2.您可以使用
list1
中的第 0 列作为合并 lsit1 和 List2 的参考。 And you need to get rid of the duplicate columns.你需要摆脱重复的列。
I know that this simple algorithm can be written in any case.我知道这个简单的算法在任何情况下都可以编写。 How to make the whole algorithm concise and clever?
如何让整个算法简洁灵巧?
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)
gives:给出:
[('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')]
This is a simple zip() application with the added twist that your lists are not ordered to match at the index level:这是一个简单的 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')
However, I would suggest using dictionaries instead of lists of tuples for this kind of "keyed" structure.但是,对于这种“键控”结构,我建议使用字典而不是元组列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.