[英]Creating a function that counts words and characters (Including punctuation, but excluding white space)
我需要創建一個函數來計算給定短語中的字符數(包括標點符號和不包括空格)和單詞數。 到目前為止,我已經創建了一個可以計算字符數的函數,但它也包括空格並且不計算單詞。 如何排除空格並實現計數單詞?
text = " If I compare myself to someone else, then I am playing a game
I will never win. "
def count_chars_words(txt):
chars = len(txt.replace(' ',''))
words = len(txt.split(' '))
return [words,chars]
print(count_chars_words(text))
output [19, 63]
通過使用replace(' ','')
從文本中去除空格來計算字符數,然后獲取字符串的長度。
通過將句子分成單詞列表並檢查列表的長度來計算單詞。
然后,在列表中返回兩者。
text ="If I compare myself to someone else, then I am playing a game I will never win."
def count_chars_words(txt):
chars = len(txt.replace(' ',''))
words = len(txt.split(' '))
return [words,chars]
print(count_chars_words(text))
輸出:
[17, 63]
要了解replace()
和split()
作用:
>> text.replace(' ','')
'IfIcomparemyselftosomeoneelse,thenIamplayingagameIwillneverwin.'
>> text.split(' ')
['If', 'I', 'compare', 'myself', 'to', 'someone', 'else,', 'then', 'I', 'am', 'playing', 'a', 'game', 'I', 'will', 'never', 'win.']
函數string.split()
可能對你有用! 它可以接受一個字符串,找到您輸入的任何內容的每個實例(例如" "
),並將您的字符串拆分為由" "
(幾乎按單詞)分隔的每組字符的列表。 有了這個,你應該能夠繼續!
"If I compare myself to someone else, then I am playing a game I will never win.".split(" ")
給
['If', 'I', 'compare', 'myself', 'to', 'someone', 'else,', 'then', 'I', 'am', 'playing', 'a', 'game', 'I', 'will', 'never', 'win.']
為了避免計算空格,您是否考慮過使用if
語句? 您可能會發現string.whitespace
和in
運算符在這里很有用!
至於數詞,string.split
是你的朋友。 事實上,如果你先把單詞分開,有沒有一種簡單的方法可以避免上面提到的if
?
這只是一個想法,而不是有效的方法,如果您需要一個好的方法來做到這一點,請使用正則表達式:
text ="If I compare myself to someone else, then I am playing a game I will never win."
total_num = len(text)
spaces = len([s for s in text if s == ' '])
words = len([w for w in text.split()])
print('total characters = ', total_num)
print('words = ', words)
print('spaces=', spaces)
print('charcters w/o spaces = ', total_num - spaces)
輸出:
total characters = 79
words = 17
spaces= 16
charcters w/o spaces = 63
編輯:使用正則表達式效率更高:
import re
chars_without_spaces = re.findall(r'[^\s]', text) # charcters w/o spaces
words = re.findall(r'\b\w+', text) # words
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.