如何从字符串构建一维列表并从列表构建字符串?
假设输入字符串为: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
答案 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的评论,如下所示。