@Jason,我建议您使用以下两种方法中的任何一种来完成此任务。
✓ 通过使用 list comprehension 和 zip() 函数创建文本列表。
注意:要在屏幕上打印\,请使用转义序列\\。见List of escape sequences and their use。
如果您认为此答案不能满足您的问题,请发表评论。我将根据您的输入和预期输出更改答案。
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() 实现上述逻辑以返回文本列表。
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"
]
"""