通过组合列表和字符串创建字符串列表

时间:2018-07-03 13:59:29

标签: python string list join zip

我正在尝试创建一个包含两个列表和一些字符串的列表:

string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]

result = ["test.1.a","test.2.b","test.3.b"]

我尝试弄乱.join和zip函数,但无济于事。

5 个答案:

答案 0 :(得分:4)

相信,可以使用zipDockerfile来实现以下pythonic方式:

list comprehension

答案 1 :(得分:4)

这有效:

['.'.join((string,)+i) for i in zip(list1, list2)]

输出:

['test.1.a', 'test.2.b', 'test.3.c']

答案 2 :(得分:1)

string = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]

lst = ['{}.{}.{}'.format(string, list1[x], list2[x]) for x in range(len(list1))]

print(lst)

输出

['test.1.a', 'test.2.b', 'test.3.c']

答案 3 :(得分:0)

只需使用concatenation技巧:

string1 = "test"
list1 = ["1","2","3"]
list2 = ["a","b","c"]

result = [(string1+"."+list1[i]+"."+list2[i]) for i in range(len(list1))]
print(result)

输出:

['test.1.a', 'test.2.b', 'test.3.c']

答案 4 :(得分:0)

列表理解:

string = "test"
list(map(lambda x,y: string+"."+x+"."+y, list1,list2))

输出:

['test.1.a', 'test.2.b', 'test.3.c']