Home > Software engineering >  Combining two lists and having it repeat based on the input of the first list
Combining two lists and having it repeat based on the input of the first list

Time:08-11

I have these two lists of the same size:

list1 = [5, 1, 20, 19, 1, 1, 1, 1, 1] 
list2 = ['29888.20','29888.20','34298.20','35724.20','36014.20','37924.20','43886.20','54282.20', '79417.20']

The first list if you add all the indexes = 50.

5 1 20 ....

My question is I want to combine the two lists.

Where 29888.20 from the first list should be repeated 5 times from list 1 and so forth.

Another example being: 34298.20 repeat 20 times

list1[0] = 5, list2 = 29888.20 # Have list2 value repeat 5 times

Final Result should be this:

final_list = ['29888.20','29888.20','29888.20','29888.20','29888.20', # repeat 5 times
             '29888.20' # repeat 1 time
             '34298.20', '34298.20', # repeat 20 times
             '35724.20', # repeat 19 times]

           and so forth for the rest of the items in list1 and list2

I hope my explanation is making sense. help would be greatly appreciated.

CodePudding user response:

Here is a list comprehension approach with the help of zip():

list1 = [5, 1, 20, 19, 1, 1, 1, 1, 1] 
list2 = ['29888.20', '29888.20', '34298.20', '35724.20', '36014.20', '37924.20', '43886.20', '54282.20', '79417.20']

output = [j for sub in [[y]*x for x, y in zip(list1, list2)] for j in sub]

print(output)

# ['29888.20', '29888.20', '29888.20', '29888.20', '29888.20',
#  '29888.20', '34298.20', '34298.20', '34298.20', '34298.20',
#  '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
#  '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
#  '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
#  '34298.20', '35724.20', '35724.20', '35724.20', '35724.20',
#  '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
#  '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
#  '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
#  '36014.20', '37924.20', '43886.20', '54282.20', '79417.20']

For a breakdown of the list comprehension, the inner one:

[[y]*x for x, y in zip(list1, list2)]

generates a sublist containing each item in list2 repeated n times, where n is the corresponding value for list1 in the same positions. The outer list comprehension simply flattens the list of lists into a single level list of strings.

CodePudding user response:

from itertools import chain

list1 = [5, 1, 20, 19, 1, 1, 1, 1, 1]
list2 = ['29888.20','29888.20','34298.20','35724.20','36014.20','37924.20','43886.20','54282.20', '79417.20']

res = list(chain.from_iterable([[l2]*l1 for l1, l2 in zip(list1, list2)]))

output:

['29888.20', '29888.20', '29888.20', '29888.20', '29888.20', '29888.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
 '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
 '34298.20', '34298.20', '34298.20', '34298.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
 '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
 '35724.20', '36014.20', '37924.20', '43886.20', '54282.20', '79417.20']

CodePudding user response:

How about a bit of itertools:) Probably not the fastest, but looks interesting:

from itertools import repeat, chain

list2 = ['29888.20', '29888.20', '34298.20', '35724.20', '36014.20', '37924.20', '43886.20', '54282.20', '79417.20']
list1 = [5, 1, 20, 19, 1, 1, 1, 1, 1]
result = chain(*[list(repeat(list2[idx], elem)) for idx, elem in enumerate(list1)])

print(list(result))

Output:

[
 '29888.20', '29888.20', '29888.20', '29888.20', '29888.20', '29888.20',
 '34298.20',
 '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20',
 '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '34298.20', '35724.20',
 '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
 '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20', '35724.20',
 '36014.20',
 '37924.20',
 '43886.20',
 '54282.20',
 '79417.20'
 ]
  • Related