Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历列表以更改URL?_Python_List_Loops_Url - Fatal编程技术网

Python 如何遍历列表以更改URL?

Python 如何遍历列表以更改URL?,python,list,loops,url,Python,List,Loops,Url,我想通过遍历列表来更改URL。有什么建议吗 比如说, 假设URL是www.example/chi.com URL列表为['chi'、'den'、'lac'] 我想要的输出是: www.example/chi.com www.example/den.com www.example/lac.com 这是我目前掌握的代码: url = "www.example" team = ["chi", 'den', 'lac'] dot_com = ".com

我想通过遍历列表来更改URL。有什么建议吗

比如说,

假设URL是www.example/chi.com

URL列表为['chi'、'den'、'lac']

我想要的输出是:

www.example/chi.com
www.example/den.com
www.example/lac.com
这是我目前掌握的代码:

url = "www.example"
team = ["chi", 'den', 'lac']
dot_com = ".com"
for t in team:
    print(t)


print("{}/{}{}".format(url, t, dot_com))

不幸的是,我现在的输出如下所示:

chi
den
lac
www.example/lac.com

任何帮助都将不胜感激。谢谢大家!

为什么不在循环时格式化它们:

url = "www.example"
team = ["chi", 'den', 'lac']
for t in team:
    print(f"{url}/{t}.com")

多亏了@mapf和@Jab,我能够用首选方法完成这个问题

以下是打印多个URL的答案:

url = 'www.example'

teams = ['chi', 'den', 'lac']

dot_com = '.com'

for t in teams:
    print('{}/{}{}'.format(url, t, dot_com))


print(“{}/{}{}.format(url,t,dot_-com))
放入循环中。当然,还有循环前面的
dot_com=“.com”
。(同时删除另一个打印语句。)这完全符合您的建议。谢谢@mapf的建议!非常感谢,先生您是否愿意将您的回答添加到问题中,以便我可以奖励您最佳答案?谢谢您,先生@Jab!这段代码工作得很好!=)