[英]How to split list elements as words when text and integers are present in python?
我有如下列表
['Hi','What is my commission','Yes',31289,'Are you sure?','Yes','Okay Thanks.I remember the applications submitted were atleast 50','Then how am I only getting paid for 10 contracts?','Okay.','No thanks..']
我必須將上面的列表元素拆分為單詞。 我試過如下
newUserList=[word for line in userList for word in line.split()]
它給出**error: 'int' object has no attribute 'split' **
如果列表中存在文本和 integer 值,如何將元素拆分為單詞?
如果您不想跳過字符串,請執行以下代碼,結果您將獲得列表中的單詞列表
if __name__ == "__main__":
x = ['Hi', 'What is my commission', 'Yes', 31289, 'Are you sure?', 'Yes',
'Okay Thanks.I remember the applications submitted were atleast 50',
'Then how am I only getting paid for 10 contracts?', 'Okay.', 'No thanks..']
words = []
for element in x:
if type(element) is str:
for word in element.split():
words.append(word)
print(words)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.