[英]How to extract words out of a string with no spaces in python?
我對 python 還有些陌生,所以我遇到了一個問題,我不知道如何解決這個特殊問題。
所以我們有一個像“ThisThingIsCool”或“thisthingiscool”這樣的字符串
現在我需要以某種方式制作一個類似 [This,Thing,Is,Cool] 或 [this,thing,is,cool] 的列表
目前,我正在使用 textblob 但我不確定他們是否有這樣的方法來做這樣的事情。
我的意思是我下載了語料庫(我猜它是一個單詞列表),但沒有看到任何 function 來識別亂碼字符串中的單詞並提取單詞。 留下一個列表作為 output。
所以我想解決至少能夠用大寫字母分割一個。 但是我不知道如何在 python 中了解 go。
所以問題是
如何識別大寫字母?
如何在不消耗分隔符的情況下拆分它?
textblob 中是否有已經這樣做的東西?
謝謝你
對於正則表達式,使用大寫字母進行拆分非常容易:
s = "ThisThingIsCool"
re.findall(r'[A-Z][^A-Z]*', s)
#['This', 'Thing', 'Is', 'Cool']
通用解決方案要困難得多,可能需要動態編程。
使用re
模塊。
>>> a = 'ThisThingIsCool'
>>> import re
>>> re.findall(r'[A-Z][a-z]*', a)
['This', 'Thing', 'Is', 'Cool']
>>> [i.lower() for i in re.findall(r'[A-Z][a-z]*', a)]
['this', 'thing', 'is', 'cool']
>>> list(map(str.lower, re.findall(r'[A-Z][a-z]*', a)))
['this', 'thing', 'is', 'cool']
s = "這是我的名字" new_s = s.split() print(new_s)
['這是我的名字']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.