如何用Python程式設計,實現輸入多個單詞,按單詞長度排序輸出並計算每個字母出現的頻率

時間 2021-12-21 18:44:43

1樓:

以下是其中一種方式供參考,最重要是找到合適的資料結構

#!/usr/bin/python

# --coding:utf-8--

input_str = input("請輸入多個單詞,以空格隔開:")

input_list = input_str.split(' ')

word_list =

char_dic = {}

for word in input_list:

#按照單詞,單詞長度組成元組,放到列表中

word_tuple = (word, len(word))

for char in wordif char in char_dicchar_dic[char] += 1

elsechar_dic[char] = 1

#根據元組中的第乙個元素進行排序

word_list.sort(key=lambda elem: elem[1])

#按照單詞由短到長輸出,單詞是元組中的第1個元素

for word in word_list:

print(word[0])

#輸出每個字元出現的次數

for key, value in char_dic.items():

print('字母 %s 出現 %s 次' % (key, value))

請輸入多個單詞,以空格隔開:this is a testais

this

test

字母 t 出現 3 次字母 h 出現 1 次字母 i 出現 2 次字母 s 出現 3 次字母 a 出現 1 次字母 e 出現 1 次

2樓:劉靜好

答「專業修航母」知友:

為什麼也向那些癖好哪壺不開提哪壺,我不關注什麼他就樂衷提問什麼的牛批人物學習不得了的提問技巧呢?

你眼神好,應該一眼看出我是不學無術之輩,高深知識不會。

尤其,航母?!⊙⊙

業餘都不會修航母的人太多了,我是永遠的其一。敢與專業修航母的掰扯Python?那是自己不知道天高地厚海闊天空……

3樓:張文迪

import

collectionss=

input

()lis=s

.split

()counter

=collections

.Counter

(lis

)result=[(

word

,nums

)for

word

,nums

incounter

.items

()]result

.sort

(key

=lambdax:

len(x[

0]))

print

(result)

如何用python實現行列互換?

程式設計小白一枚,使用python 3.6 笨辦法 b fori inrange len a 0 row forj inrange len a row.a j i b.row print b 以上寫成一句 print row i for rowina fori inrange len a 0 用函式...

如何用python實現SVD分解呢?

將陣列按第一行從大到小排序 order lambdaA A.T np.argsort A.T 0 T 補齊列空間的單位正交基 add null lambdaU np.row stack U T,scipy linalg null space U T T T defsvd A m,n A.shape ...

如何用Haskell實現物件導向程式設計?

圓角騎士魔理沙 剛剛讀完了Haskell s overlooked object system,給出了幾個proposal,最後深入研究用HList encode recursive record。很有意思,比如說,class,label是first class的,所以多重繼承玩得很溜,比如說可以自...