python中用于多个字符串的类paste0函数
我想要实现的很简单,在R中我可以做如下事情python中用于多个字符串的类paste0函数,python,string-concatenation,Python,String Concatenation,我想要实现的很简单,在R中我可以做如下事情 paste0(“https\\”,1:10,“随便什么”,11:20) 如何在Python中实现这一点?我发现了一些问题,但只考虑: paste0(“https\\”,1:10) 任何人都知道如何解决这个问题,这一定很容易,但我找不到方法。根据您提供的链接,这应该可以: ["https://" + str(i) + "whatever" + str(i) for i in xrange(1,11)] 提供以下输出: ['https://1whatev
paste0(“https\\”,1:10,“随便什么”,11:20)
如何在Python中实现这一点?我发现了一些问题,但只考虑:
paste0(“https\\”,1:10)
任何人都知道如何解决这个问题,这一定很容易,但我找不到方法。根据您提供的链接,这应该可以:
["https://" + str(i) + "whatever" + str(i) for i in xrange(1,11)]
提供以下输出:
['https://1whatever1', 'https://2whatever2', 'https://3whatever3',
'https://4whatever4', 'https://5whatever5', 'https://6whatever6',
'https://7whatever7', 'https://8whatever8',
'https://9whatever9', 'https://10whatever10']
编辑:
这应该适用于粘贴0(“https\\”,1:10,“随便什么”,11:20)
@Jason,我建议您使用以下两种方法中的任何一种来完成此任务 ✓ 通过使用列表理解和zip()函数创建文本列表 注意:要在屏幕上打印
\
,请使用转义序列\
。看
如果你认为这个答案不能满足你的问题,请发表评论。我将根据您的输入和预期输出更改答案
✓ 通过定义一个简单函数paste0(),该函数实现上述逻辑以返回文本列表
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"
]
"""
您好@ResetACK,谢谢您的回复,这确实有效。很抱歉,我没有把问题说清楚,如果它改为粘贴0(“https\\”,1:10,“whatever”,11:20),该怎么办?BYW,也在问题中编辑。@JasonGoal编辑了新的答案,让我知道你是怎么做的go@ResetACKm,你的解决方案对我很有效。Rishikesh Agrawani的答案更容易实现,特别是第一个答案,所以我将把功劳归于他。无论如何,感谢您提供的解决方案。您可以将列表理解与zip()结合使用来解决此问题。感谢,一个可行且易于实现的解决方案,将此作为答案。感谢您的宝贵反馈@Jason。
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
"""
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"
]
"""