PYTHON:从字符串返回一维列表,反之亦然

时间:2012-02-24 11:02:45

标签: python-3.x

如何从字符串构建一维列表并从列表构建字符串?

假设输入字符串为:STACKOVERFLOW
那么输出列表应为outputList = [S,T,A,C,K,O,V,E,R,F,L,O,W]

如果输入列表inputList = [S,T,A,C,K,O,V,E,R,F,L,O,W]
输出应为string = STACKOVERFLOW

1 个答案:

答案 0 :(得分:2)

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "STACKOVERFLOW"
>>> l = [c for c in s] # Or just list(s), as noted below by agf
>>> l
['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W']
>>> ''.join(l)
'STACKOVERFLOW'

已编辑以显示agf的评论,如下所示。