Python記錄標點符號和單詞的數量

2021-10-06 08:58:45 字數 1389 閱讀 2930

首先要求使用者輸入乙個正整數n,然後統計下面這段英文中標點符號和單詞出現的次數,根據使用者輸入的正整數n,按降序列印出現次數最多的n個標點符號和單詞(先列印標點符號,再列印單詞,列印的標點符號和單詞之間下劃線連線)。

備註:標點符號指的是在string模組punctuation中的字元。先導入string庫:import string,string.punctuation 就返回標點符號字串。

s為原文本,已省去

import string

number =

input

('請輸入乙個正整數:'

)try

:eval

(number)

except nameerror :

print

('請輸入乙個正整數'

)else:if

type

(eval

(number))!=

int:

print

('error:請輸入乙個正整數'

)else

: number=number

count1=

count2=

s=s.lower(

)for i in s:

if i in string.punctuation:

count1[i]

=count1.get(i,0)

+1else

:continue

for i in s:

if i in string.punctuation:

s=s.replace(i,'')

words=s.split(

)for word in words:

count2[word]

=count2.get(word,0)

+1list1=

list

(count1.items())

list2=

list

(count2.items())

list1.sort(key=

lambda x: x[1]

,reverse=

true

) list2.sort(key=

lambda x: x[1]

,reverse=

true

) list3=

list4=

for i in

range

(eval

(number)):

[0])

[0])

list5=list3+list4

print

('_'

.join(list5)

)

英語標點符號大全

close brace,close curly 右花括號 open parenthesis,open paren 左圓括號 close parenthesis,close paren 右圓括號 brakets parentheses 括號 open bracket 左方括號 close bracke...

標點符號英文寫法

period 句號 ma 逗號 colon 冒號 semicolon 分號 exclamation 驚嘆號 question mark 問號 hyphen 連字元 apostrophe 省略號 所有格符號 dash 破折號 single quotation marks 單引號 double quot...

標點符號相關詞彙

逗號 comma 句號 full stop 頓號 pause mark 冒號 colon 分號 semicolon 問號 question mark 省略號 suspension points 嘆號 exclamation mark 書名號 book title mark 引號 quote 前括號 ...