本文目錄一覽:
- 1、Python練習題,應該是jieba的應用,但是我不太會寫,希望有人指導一下,感謝!!
- 2、關於Python字典的兩道題目
- 3、Python字典題求助
- 4、急求!這道python字典函數的題怎麼做呀?
Python練習題,應該是jieba的應用,但是我不太會寫,希望有人指導一下,感謝!!
str = input(“請輸入要分析的字符串,回車表示結束:”)
while str != ”:
# 創建字典類型保存結果
counts = {}
# 創建字典類型
for ch in str:
counts[ch] = counts.get(ch,0) + 1
# 改變類型為列表類型,按照出現頻率降序排列
items = list(counts.items())
# 利用sort函數排序
items.sort(key= lambda x : x[1],reverse= True)
# 打印輸出。
for i in range(len(items)):
word, count = items[i]
print(“{0:10}{1:5}”.format(word, count))
str = input(“請輸入要分析的字符串,回車表示結束:”)
關於Python字典的兩道題目
dd = dict()
while True:
key = input(“存入的鍵:”)
value = input(“存入的值:”)
dd[key] = value
flag = input(“是否繼續存入[y/Y/N/n]:”)
if flag in [‘n’,’N’]:
break
keys = list(dd.keys())
values = list(dd.values())
print(keys)
print(values)
我的是3版本,自己改改,輸入錯誤看看是不是數據類型問題
第五題:
letter = dict()
strs = input(“輸入一串字符串:”)
for i in strs:
letter.setdefault(i,0)
letter[i] += 1
keys = list(letter.keys())
keys.sort()
for i in keys:
print(i,letter[i])
Python字典題求助
def displayDocument(d):
print(‘歸檔檔案清單:’)
for k, v in d.items():
print(f'{v} {k}’)
total = sum(d.values())
print(f’已歸檔檔案共計:{total} 件’)
displayDocument(d)
急求!這道python字典函數的題怎麼做呀?
等於8
get方法 第一個參數是key ,獲取得到就返回字典里對應的value,獲取不到就返回第二個參數,
所以第一個get得到3 第二個get得到5
加起來是8
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/297908.html