我有一份清单 - [1,2,3,4] 和一个网址 - https://www.somewebsite.com
我想创建一个for循环来迭代列表元素,并添加到url的末尾。
示例... https://www.somewebsite.com/1,https://www.somewebsite.com/2等
感谢您的帮助,
答案 0 :(得分:0)
试试这个:
a_list = [1, 2, 3, 4]
a_url = 'https://www.somewebsite.com'
urls = []
for n in a_list:
url = '{}/{}'.format(a_url, n)
urls.append(url)
>>> urls
['https://www.somewebsite.com/1', 'https://www.somewebsite.com/2', 'https://www.somewebsite.com/3', 'https://www.somewebsite.com/4']
答案 1 :(得分:0)
您可以使用列表理解在一行中执行此操作:
list_1=[1, 2, 3, 4]
url='https://www.somewebsite.com'
print(["{}/{}".format(url,i) for i in list_1])
输出:
['https://www.somewebsite.com/1', 'https://www.somewebsite.com/2', 'https://www.somewebsite.com/3', 'https://www.somewebsite.com/4']