Python:将列表转为字符串的3种方法

#一天一个Python小技巧#

将列表转为字符串:

1、使用for循环

testlist = ['h','e','l','l','o']
teststr =''
for i in testlist:
    teststr += i 
print(teststr)

2、join方法:

testlist = ['h','e','l','l','o']
teststr = "".join(testlist)
print(teststr)

3、reduce方法:

from functools import reduce
testlist = ['h','e','l','l','o']
teststr = reduce(lambda x, y: x+y, testlist)
print(teststr)
PS C:\Users\farew> & D:/ANACONDA/python.exe c:/Users/farew/Desktop/Untitled-1.py
hello
 

你可能感兴趣的:(python,字符串,列表,生成器,mapreduce)