假設你想在一個句子計算終端標點符號,我們可以通過遍歷每個字符串的字符及過濾標點符號產生的字典(字符,計數)對。
演示
這裏有三個選項呈現自上而下與中程對初學者級別的數據結構:
import collections as ct
sentence = "Here is a sentence, and it has some exclamations!!"
terminals = ".?!"
# Option 1 - Counter and Dictionary Comprehension
cd = {c:val for c, val in ct.Counter(sentence).items() if c in terminals}
cd
# Out: {'!': 2}
# Option 2 - Default Dictionary
dd = ct.defaultdict(int)
for c in sentence:
if c in terminals:
dd[c] += 1
dd
# Out: defaultdict(int, {'!': 2})
# Option 3 - Regular Dictionary
d = {}
for c in sentence:
if c in terminals:
if c not in d:
d[c] = 0
d[c] += 1
d
# Out: {'!': 2}
爲了進一步擴展,爲獨立sentences
列表,一環周圍後者的選擇。
for sentence in sentences:
# add option here
注:總結每個句子的總標點符號,總的dict.values()
,例如sum(cd.values())
。
更新:假設你要通過終端punctutation拆分句子,使用正則表達式:
import re
line = "Here is a string of sentences. How do we split them up? Try regular expressions!!!"
# Option - Regular Expression and List Comprehension
pattern = r"[.?!]"
sentences = [sentence for sentence in re.split(pattern, line) if sentence]
sentences
# Out: ['Here is a string of sentences', ' How do we split them up', ' Try regular expressions']
len(sentences)
# Out: 3
通知line
有5個終端,但是隻有3句。因此正則表達式是一種更可靠的方法。
參考
你需要爲他們單獨計數?這裏有什麼用途 - 計算標點符號或計算句子的數量? 「因爲這不是5句話!!!!!」 –
在這裏什麼是「內置函數」?你可以使用標準庫模塊嗎? –
對不起。我的意思是方法。舉個例子,如果有一些可能會自動剝離它,然後我們是不允許使用它e.g如果我們選,我們不允許使用variable.sort() –