对于多个字符串,python 中的类似 paste0 的函数 [英] paste0 like function in python for multiple strings
问题描述
我想要实现的很简单,在 R 中我可以做类似的事情
What I want to achieve is simple, in R I can do things like
paste0("https\\",1:10,"whatever",11:20)
,
如何在 Python 中做到这一点?我在这里找到了一些东西,但只允许:
how to do such in Python? I found sme things here, but only allow for :
paste0("https\\",1:10)
.
任何人都知道如何解决这个问题,这一定很容易做到,但我找不到方法.
anyone know how to figure this out, this must be easy to do but I can not find how.
推荐答案
@Jason,我建议您使用以下 2 种方法中的任何一种来完成此任务.
@Jason, I will suggest you to use any of these following 2 ways to do this task.
✓ 通过使用列表理解和zip()函数创建文本列表.
✓ By creating a list of texts using list comprehension and zip() function.
注意:要在屏幕上打印\
,请使用转义序列\\
.请参阅转义序列列表及其用途.
Note: To print
\
on screen, use escape sequence\\
. See List of escape sequences and their use.
如果您认为此答案不能满足您的问题,请发表评论.我会根据您的输入和预期输出更改答案.
Please comment if you think this answer doesn't satisfy your problem. I will change the answer based on your inputs and expected outputs.
texts = ["https\\\\" + str(num1) + "whatever" + str(num2) for num1, num2 in zip(range(1,10),range(11, 20))]
for text in texts:
print(text)
"""
https\\1whatever11
https\\2whatever12
https\\3whatever13
https\\4whatever14
https\\5whatever15
https\\6whatever16
https\\7whatever17
https\\8whatever18
https\\9whatever19
"""
✓ 通过定义一个简单的函数 paste0() 来实现上述逻辑以返回文本列表.
✓ By defining a simple function paste0() that implements the above logic to return a list of texts.
import json
def paste0(string1, range1, strring2, range2):
texts = [string1 + str(num1) + string2 + str(num2) for num1, num2 in zip(range1, range2)]
return texts
texts = paste0("https\\\\", range(1, 10), "whatever", range(11, 20))
# Pretty printing the obtained list of texts using Jon module
print(json.dumps(texts, indent=4))
"""
[
"https\\\\1whatever11",
"https\\\\2whatever12",
"https\\\\3whatever13",
"https\\\\4whatever14",
"https\\\\5whatever15",
"https\\\\6whatever16",
"https\\\\7whatever17",
"https\\\\8whatever18",
"https\\\\9whatever19"
]
"""
这篇关于对于多个字符串,python 中的类似 paste0 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!