将两个列表组合成字符串 [英] Combining two lists to string
本文介绍了将两个列表组合成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上,我正在尝试将两个列表合并为一个字符串,但要使它们保持有序含义:
Actually I'm trying to merge two lists to one string but keep them ordered meaning:
list1 = [1,2,3,4,5]
list2 = ["one", "two", "three", "four", "five"]
result = "1one2two3three4four5five"
(列表的长度始终相同,但内容有所不同)
(lists always have the same length but vary in contents)
此刻我正在这样做:
result = ""
i = 0
for entry in list1:
result += entry + list2[i]
i += 1
我认为必须有一种更Python化的方式来做到这一点,但我实际上并不知道.
I think there must be a more pythonic way to do this but I don't know it actually.
愿你们中的某人可以帮助我解决这个问题.
May someone of you can help me out on this.
推荐答案
list1 = [1,2,3,4,5]
list2 = ["one", "two", "three", "four", "five"]
print ''.join([str(a) + b for a,b in zip(list1,list2)])
1one2two3three4four5five
这篇关于将两个列表组合成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文