简体   繁体   English

如何使用 python 的“列表”添加新列并找到元素之间的对应关系?

[英]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 .现在我们有了list1list2 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.注意list1list2的列数不是固定的,行数必须相等。 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.

相关问题 两个列表元素之间的关系:如何在Python中利用它? - Relationship between elements of two list: how to exploit it in Python? Python-如何使用计数器将列元素添加到列表 - Python - How to add column elements to a list with a counter 如何在python中添加两个dict的对应元素? - How to add corresponding elements of two dicts in python? 如何在python中添加2个多维矩阵的对应元素? - How to add corresponding elements of 2 multidimensional matrices in python? Python:在列表中找到对应的元素并将它们汇总到新列表中 - Python: Find corresponding elements in a list and sum them up in a new list Python 3 Pandas:如何找到一列的行元素子集与另一列的元素之间的关系? - Python 3 Pandas: How can I find the relationship between a subset of row elements for one column and elements of another column? 如果上一列中的对应项在列表中,则将新列添加到 pandas dataframe - Add a new column to a pandas dataframe if the corresponding item in the previous column is in a list 在python列表中查找元组之间的公共元素 - Finding common elements between tuples in a list in python 查找增量 - Python 列表中元素之间的差异 - Finding Deltas - Difference between elements in a Python list Python:查找列表元素之间的差异 - Python: Finding differences between elements of a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM