zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python:将列表转为字符串的3种方法「建议收藏」

Python列表方法 建议 收藏 字符串 转为
2023-06-13 09:11:26 时间

大家好,又见面了,我是你们的朋友全栈君。

#一天一个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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141887.html原文链接:https://javaforall.cn