Multiplying a list of integer with a list of string
Asked Answered
C

8

5

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?

Civic answered 29/7, 2022 at 9:5 Comment(0)
O
4

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']
Overbalance answered 29/7, 2022 at 9:10 Comment(0)
P
5

you can (abuse) sum:

>>> sum([[a] * b for a, b in zip(l2, l1)], [])
['a', 'a', 'b', 'b', 'c', 'c', 'c']
Preconize answered 29/7, 2022 at 9:16 Comment(0)
O
4

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']
Overbalance answered 29/7, 2022 at 9:10 Comment(0)
C
4

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']
Cuticula answered 29/7, 2022 at 9:11 Comment(1)
for c in a * b only works if strings are single characters.Horse
C
3

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']
Clemmer answered 29/7, 2022 at 9:16 Comment(0)
M
2

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']
Mccarter answered 29/7, 2022 at 9:12 Comment(0)
C
2

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

Carr answered 29/7, 2022 at 9:41 Comment(0)
S
1

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
Sheepshead answered 29/7, 2022 at 9:14 Comment(2)
list(s * i) only works for single characters; better use [s] * iHorse
Yes that's a more general way in case of non-single character strings.Sheepshead
B
0

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
Britain answered 29/7, 2022 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.