Alex M
Alex M

Reputation: 187

How to combine a string list with a list of lists of integers

I would like to combine the following string list and integer list of lists:

lst = ['A', 
       'A', 
       'B', 
       'C', 
       'C', 
       'D', 
       'D', 
       'D',....]

lst_of_lst = [[9, 10, 11, 12],
              [54, 55, 56],
              [72, 73, 74, 75, 76], 
              [1, 2, 3, 4, 5],
              [98, 99, 100],
              [13, 14],
              [21, 22, 23],
              [27, 28, 29, 30], ....]

such that a list of tuples is returned:

lst_tups = [('A', 9), ('A', 10), ('A', 11), ('A', 12),
            ('A', 54), ('A', 55), ('A', 56), 
            ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76),
            ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5), 
            ('C', 98), ('C', 99), ('C', 100), 
            ('D', 13), ('D', 14),
            ('D', 21), ('D', 22), ('D', 23),
            ('D', 27), ('D', 28), ('D', 29), ('D', 30), ....]

The 2 lists have the same number of list elements (in the test case above - 8). Unfortunately, using a dictionary strategy is out of the question due to duplicate string entries in lst.

I have tried the following, which only works for the first element of each sublist in lst_of_lst and repeats for each string in lst:

empty_test_combo = []
for x in helix_chain_id:
    for y in helix_seq_res_num_ranges:
        empty_test_combo += (zip(x, y))

I have also tried:

lst_tups = []
for x in lst:
    for y in lst_of_lst:
        for z in y:
            lst_tups.append(zip(x, [z]))

This seems the most promising option. It returns a list of tuples that combines the lst strings and lst_of_lst integer lists correctly, but only partially.

Upvotes: 1

Views: 1561

Answers (1)

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

It seems that there is a misunderstanding in the way you try to use zip.

zip(list1, list2) is an iterator. When you iterate on it, you get tuples: the first one is made of the first item of list1 and the first item of list2, and so on.

What you want to do here is to zip(lst, lst_of_lst) in order to pair each element of lst to the corresponding sublist of lst_of_lst. From each pair, you can generate the output you want.

You can do that with a list comprehension:

lst = ['A', 'A', 'B', 'C', 'C', 'D', 'D', 'D',]

lst_of_lst = [[9, 10, 11, 12],
              [54, 55, 56],
              [72, 73, 74, 75, 76], 
              [1, 2, 3, 4, 5],
              [98, 99, 100],
              [13, 14],
              [21, 22, 23],
              [27, 28, 29, 30],]


out = [(item1, item2) for item1, sublist in zip(lst, lst_of_lst) for item2 in sublist]

print(out)
# [('A', 9), ('A', 10), ('A', 11), ('A', 12), ('A', 54), ('A', 55), ('A', 56), 
#  ('B', 72), ('B', 73), ('B', 74), ('B', 75), ('B', 76), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5),
#  ('C', 98), ('C', 99), ('C', 100), ('D', 13), ('D', 14), ('D', 21), ('D', 22), ('D', 23), 
#  ('D', 27), ('D', 28), ('D', 29), ('D', 30)]

Or, written with loops, as you tried:

out = []
for item1, sublist in zip(lst, lst_of_lst):
    for item2 in sublist:
        out.append((item1, item2))

Upvotes: 1

Related Questions