列表上的python操作

时间:2016-09-30 03:16:20

标签: python string list data-manipulation

我有2个列表

l1 = ('A','B','C')
l2 = ('X','Y','Z')

我想根据这些2

创建一个列表

result =('A与X'相同,'B与Y'相同,'C与Z相同')

当我连接时,我没有得到我期望的结果 如何组合列表? 谢谢 PMV

3 个答案:

答案 0 :(得分:2)

zip()函数可以帮助你。

result = []
for a, b in zip(l1, l2):
    result.append("{0} is same as {1}".format(a, b))

答案 1 :(得分:0)

您可以zip将两个列表组合在一起的列表,然后迭代这些元组。

z = zip(l1,l2) #[(A,X), (B,Y), (C,Z)]

result = ["{0} is the same as {1}".format(t[0], t[1]) for t in z]

# ['A is the same as X', 'B is the same as Y', 'C is the same as Z']

答案 2 :(得分:0)

更直接的方式......

>>> map('{} is same as {}'.format, l1, l2)
['A is same as X', 'B is same as Y', 'C is same as Z']