2024-04-24 22:00:15 发布
网友
我正在尝试将一个句子转换成没有空格分隔符的列表格式 我试过下面的方法
a = 'this is me'
当我使用split进入列表格式时
a.split(' ') # ['this', 'is', 'me'] list(a) # ['t','h','i','s','m','e']
有没有什么方法可以作为一种输入
并将输出作为
a = ['this is me']
" ".join(a.split(" "))将返回'this is me',如果a有前导/尾随空格a = ' this is me ',则可以使用" ".join(a.strip().split(" "))
" ".join(a.split(" "))
'this is me'
a
a = ' this is me '
" ".join(a.strip().split(" "))
为了得到你的解决方案
a = [a.strip()]
使用本文件:你知道吗
>>> a = 'this is me' >>> [a] ['this is me']
使用list使函数在字符串上迭代,这是您不想要的。改用那些大括号,作为列表构造函数。你知道吗
list
a = 'this is me'
" ".join(a.split(" "))
将返回'this is me'
,如果a
有前导/尾随空格a = ' this is me '
,则可以使用" ".join(a.strip().split(" "))
为了得到你的解决方案
使用本文件:你知道吗
使用
list
使函数在字符串上迭代,这是您不想要的。改用那些大括号,作为列表构造函数。你知道吗相关问题 更多 >
编程相关推荐