简体   繁体   English

将整个句子转换为字符串到列表中而不进行拆分

[英]converting entire sentence into string into list without split

I am trying to convert a sentence into list format without space separator I tried the following way我正在尝试将句子转换为没有空格分隔符的列表格式我尝试了以下方式

a = 'this is me'

when I am using split to get into list format当我使用 split 进入列表格式时

a.split(' ')
# ['this', 'is', 'me']

list(a)
# ['t','h','i','s','m','e']

is there any way to give input as有什么办法可以输入

a = 'this is me'

and get the output as并获得 output 作为

a = ['this is me']

Use this:-用这个:-

>>> a = 'this is me'
>>> [a]
['this is me']

Using list made the function iterate on the string, which you didn't want.使用list使 function 迭代字符串,这是您不想要的。 Use those big braces instead, as a list constructor.改为使用那些大括号作为列表构造函数。

IN: a= 'this is me'
IN: a = [a]
IN: print(a)
OUT: ['this is me']

a = 'this is me'

" ".join(a.split(" ")) will return 'this is me' and if a has leading/trailing spaces a = ' this is me ' you can use " ".join(a.strip().split(" ")) " ".join(a.split(" "))将返回'this is me'如果a有前导/尾随空格a = ' this is me '你可以使用" ".join(a.strip().split(" "))

to get your solution do得到你的解决方案

a = [a.strip()]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM