将两个列表组合成字符串

chi*_*l0r 9 python string list

实际上我正在尝试将两个列表合并为一个字符串,但保持它们的有序意义:

list1 = [1,2,3,4,5]
list2 = ["one", "two", "three", "four", "five"]

result = "1one2two3three4four5five"
Run Code Online (Sandbox Code Playgroud)

(列表总是长度相同但内容不同)

目前我正是这样做的:

result = ""
i = 0

for entry in list1:
    result += entry + list2[i]
    i += 1
Run Code Online (Sandbox Code Playgroud)

我认为必须有更多的pythonic方法来做到这一点但我实际上并不知道.

愿你们中的某些人可以帮助我解决这个问题.

ars*_*jii 14

list1 = [1,2,3,4,5]
list2 = ["one", "two", "three", "four", "five"]

print ''.join([str(a) + b for a,b in zip(list1,list2)])
Run Code Online (Sandbox Code Playgroud)
1one2two3three4four5five

  • 请注意,列表推导只是一个更好的选择,你知道整个事情将会耗尽. (2认同)