Home > Software engineering >  Multiplying a list of integer with a list of string
Multiplying a list of integer with a list of string

Time:07-30

Suppose there are two lists:

l1 = [2,2,3]
l2 = ['a','b','c']

I wonder how one finds the product of the two such that the output would be:

#output: ['a','a','b','b','c','c','c']

if I do:

l3 = []
for i in l2:
    for j in l1:
        l3.append(i)

I get:

['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']

which is wrong, I wonder where am I making the mistake?

CodePudding user response:

The loop for j in l1: will iterate 3 times every time (because you have 3 items in list l1).

Try:

out = [b for a, b in zip(l1, l2) for _ in range(a)]
print(out)

Prints:

['a', 'a', 'b', 'b', 'c', 'c', 'c']

CodePudding user response:

you can (abuse) sum:

>>> sum([[a] * b for a, b in zip(l2, l1)], [])
['a', 'a', 'b', 'b', 'c', 'c', 'c']

CodePudding user response:

If you multiply a string with an integer, it just repeats that string: e.g. 'a' * 3 outputs 'aaa'.

So, with a list comprehension:

out = [b for a, b in zip(l1, l2) for _ in range(a)]
print(out)

Output:

['a', 'a', 'b', 'b', 'c', 'c', 'c']

CodePudding user response:

In Python, you an directly multiply a string by a number:

l1 = [2,2,3]
l2 = ['a','b','c']


res = [c for a, b in zip(l2, l1) for c in a * b]
print(res)

Output

['a', 'a', 'b', 'b', 'c', 'c', 'c']

As a functional programming alternative:

from itertools import chain
from operator import mul

res = list(chain.from_iterable(map(mul, l2, l1)))

print(res)

Output

['a', 'a', 'b', 'b', 'c', 'c', 'c']

CodePudding user response:

With only 1 for loop :

res = []
for i in range(len(l1)): res  = [l2[i]] * l1[i]
print(res)

Output :

['a', 'a', 'b', 'b', 'c', 'c', 'c']

CodePudding user response:

You can do it like this :

l1 = [2,2,3]
l2 = ['a','b','c']

# You can zip the two lists together then create a list by multiplying 
#their elements then converting back to a list like so :
l = [ list([s] * i) for i, s in zip(l1, l2)]
output = [item for sublist in l for item in sublist]
output

CodePudding user response:

You could use itertools.repeat to repeat the string you want:

>>> from itertools import repeat

>>> l1 = [2, 2, 3]
>>> l2 = ['a', 'b', 'c']

>>> [v for n, s in zip(l1, l2) for v in repeat(s, n)]
['a', 'a', 'b', 'b', 'c', 'c', 'c']

This works even if l2's substrings are more than one character.

I like this way of doing it as it's a bit more readable (to me at least!).

CodePudding user response:

Hi William there are above very good answers, what i tried was to create function that will take your number list and char list and produce new list with each char added with the amount you specify in number list:

enter code here
l1 = [2,2,3]
l2 = ['a','b','c']

def new_list(num_list, char_list):
    l3 = []
    for i in range(0,len(num_list)):
        amount_to_append = num_list[i]
        item_to_be_added = char_list[i]
        for i in range(0,amount_to_append):
            l3.append(item_to_be_added)
    return l3
  • Related