如何将列表中的字符串分开,乘以一个数字

huangapple go评论82阅读模式
英文:

How to separate strings in a list, multiplied by a number

问题

你的代码需要将列表中的每个项目都乘以4,并用逗号分隔它们。你需要的输出是每个表达式重复显示的次数乘以4,用逗号分隔。

以下是修改后的代码:

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 = [item for item in conc for _ in range(4)]
result = ', '.join(new_conc)
print(result)

这段代码将输出你所需的结果。

英文:

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.

答案1

得分: 3

你可以使用简单的 for 循环。

new_conc = []
for item in conc:
    new_conc.extend([item] * 4)
英文:

You can use a simple for loop.

new_conc = []
for item in conc:
    new_conc.extend([item] * 4)

答案2

得分: 1

你可以尝试使用双重循环:

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)

或者使用一行代码的方式:

out = [item for item in conc for _ in range(4)]

或者(如果输出中的顺序不重要):

out = conc * 4
英文:

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

答案3

得分: 1

你不能直接将字符串相乘,你需要从非数字部分分离出数值部分,然后进行相乘,最后重新将数值部分和字符串部分合并。

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)
英文:

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)

答案4

得分: 0

以下是翻译好的部分:

"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:"

"But, if you, guys, know better solution, I will be happy to hear it!"

英文:

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([
展开收缩
* n for s, n in zip(conc, b)], []) print(c)

But, if you, guys, know better solution, I will be happy to hear it!

huangapple
  • 本文由 发表于 2023年2月24日 05:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定