kireirain
kireirain

Reputation: 35

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. 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. 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.

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:

[("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. You can use column 0 in list1 as a reference to merge lsit1 and 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?

Upvotes: 0

Views: 44

Answers (2)

Alain T.
Alain T.

Reputation: 42143

This is a simple zip() application with the added twist that your lists are not ordered to match at the index level:

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.

Upvotes: 1

Lior Cohen
Lior Cohen

Reputation: 5745

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')]

Upvotes: 1

Related Questions