How to separate strings in a list, multiplied by a number
Question:
I need to take a list, multiply every item by 4 and separate them by coma.
My code is:
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
new_conc = [", ".join(i*4) for i in conc]
print(new_conc)
But when I run it, I get every SYMBOL separated by come. What I need is multiplied number of shown EXPRESSIONS separated by coma.
So the output should be:
['0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml']
I found this answered question, but as I already mentioned, I get separate symbols, separated by coma.
Answers:
You can try double for-loop:
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
out = []
for item in conc:
for _ in range(4):
out.append(item)
print(out)
Or one-liner:
out = [item for item in conc for _ in range(4)]
Or (if order as you stated in your output is not important):
out = conc * 4
Okay, when I despaired of finding an answer and posted this question, I found this answered question. 🙂
So the solution to my problem can be:
b = [4, 4, 4, 4]
c = sum([[s] * n for s, n in zip(conc, b)], [])
print(c)
But, if you, guys, know better solution, I will be happy to hear it!
You can use a simple for
loop.
new_conc = []
for item in conc:
new_conc.extend([item] * 4)
Welcome to StackOverFlow! As correctly pointed out by other users, you can’t directly multiply a string, you need to "separate" the numeric part from the non-numeric one, then you can perform your multiplication, and finally rejoin the numeric and the string parts.
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
multiplied_conc = [': '.join([f"{float(p.split()[0])*4} {p.split()[1]}" for p in c.split(' : ')]) for c in conc]
print(multiplied_conc)
I need to take a list, multiply every item by 4 and separate them by coma.
My code is:
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
new_conc = [", ".join(i*4) for i in conc]
print(new_conc)
But when I run it, I get every SYMBOL separated by come. What I need is multiplied number of shown EXPRESSIONS separated by coma.
So the output should be:
['0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml']
I found this answered question, but as I already mentioned, I get separate symbols, separated by coma.
You can try double for-loop:
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
out = []
for item in conc:
for _ in range(4):
out.append(item)
print(out)
Or one-liner:
out = [item for item in conc for _ in range(4)]
Or (if order as you stated in your output is not important):
out = conc * 4
Okay, when I despaired of finding an answer and posted this question, I found this answered question. 🙂
So the solution to my problem can be:
b = [4, 4, 4, 4]
c = sum([[s] * n for s, n in zip(conc, b)], [])
print(c)
But, if you, guys, know better solution, I will be happy to hear it!
You can use a simple for
loop.
new_conc = []
for item in conc:
new_conc.extend([item] * 4)
Welcome to StackOverFlow! As correctly pointed out by other users, you can’t directly multiply a string, you need to "separate" the numeric part from the non-numeric one, then you can perform your multiplication, and finally rejoin the numeric and the string parts.
conc = ['0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml']
multiplied_conc = [': '.join([f"{float(p.split()[0])*4} {p.split()[1]}" for p in c.split(' : ')]) for c in conc]
print(multiplied_conc)