[英]How to Iterate Through List to Change URL?
I would like to change the URL by iterating through a list.我想通过遍历列表来更改 URL 。 Any suggestions?有什么建议么?
For example,例如,
Let's say the URL is www.example/chi.com假设 URL 是 www.example/chi.com
The list of URLs is ['chi', 'den', 'lac'] URL 列表是 ['chi', 'den', 'lac']
The desired output that I am looking for is:我正在寻找的所需 output 是:
www.example/chi.com
www.example/den.com
www.example/lac.com
This is the code that I have so far:这是我到目前为止的代码:
url = "www.example"
team = ["chi", 'den', 'lac']
dot_com = ".com"
for t in team:
print(t)
print("{}/{}{}".format(url, t, dot_com))
Unfortunately the output that I have now looks like:不幸的是,我现在拥有的 output 看起来像:
chi
den
lac
www.example/lac.com
Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you!谢谢!
Why not just format them as you loop through them:为什么不只是在遍历它们时格式化它们:
url = "www.example"
team = ["chi", 'den', 'lac']
for t in team:
print(f"{url}/{t}.com")
I was able to complete this question in the preferred method thanks to @mapf and @Jab.感谢@mapf 和@Jab,我能够以首选的方法完成这个问题。
Here is the answer on printing multiple urls:这是打印多个网址的答案:
url = 'www.example'
teams = ['chi', 'den', 'lac']
dot_com = '.com'
for t in teams:
print('{}/{}{}'.format(url, t, dot_com))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.