簡體   English   中英

將整個句子轉換為字符串到列表中而不進行拆分

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

我正在嘗試將句子轉換為沒有空格分隔符的列表格式我嘗試了以下方式

a = 'this is me'

當我使用 split 進入列表格式時

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

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

有什么辦法可以輸入

a = 'this is me'

並獲得 output 作為

a = ['this is me']

用這個:-

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

使用list使 function 迭代字符串,這是您不想要的。 改為使用那些大括號作為列表構造函數。

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

a = 'this is me'

" ".join(a.split(" "))將返回'this is me'如果a有前導/尾隨空格a = ' this is me '你可以使用" ".join(a.strip().split(" "))

得到你的解決方案

a = [a.strip()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM