对于循环,追加新值

时间:2018-05-12 18:51:34

标签: python-3.x list loops for-loop

感谢您的任何建议。有我的问题:

我有一份关于公司名称的清单,让我们说:

names_of_company = ['x', 'y', 'z', 'o', 'v']

现在我想创建一个包含网站地址的新列表,其中列表中包含公司名称的位置将添加到地址中,如下所示:

links = "www.something/names/finance"

all_links = ["www.something/x/finance", "www.something/y/finance", "www.something/z/finance", "www.something/o/finance", "www.something/v/finance"

我尝试使用for循环,但所有名称都已添加,而不仅仅是一个。我将非常感谢你的帮助

最好的问候

2 个答案:

答案 0 :(得分:2)

names_of_company = ['x', 'y', 'z', 'o', 'v']
new_list = list()

for i in range(len(names_of_company)):
    new_string = 'www.something/' + names_of_company[i] + '/finance'
    new_list.append(new_string)

print(new_list)

输出:

['www.something/x/finance', 'www.something/y/finance', 'www.something/z/finance', 'www.something/o/finance', 'www.something/v/finance']

答案 1 :(得分:1)

names = ['x', 'y', 'z', 'o', 'v']
links = ['www.something/{}/finance'.format(name) for name in names]