日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Python【每日一问】36

發(fā)布時間:2025/7/14 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python【每日一问】36 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問:

基礎(chǔ)題:

809*x=800*x+9*x+1 其中 x 代表的兩位數(shù), 8*x 的結(jié)果為兩位數(shù), 9*x 的結(jié)果為 3 位數(shù)。求 x ,及計算 809*x 的結(jié)果。

提高題:

對文件"命運.txt"進(jìn)行字符頻次統(tǒng)計,并將所有字符按照頻次高低排序,將排序后的字符及其頻次輸出到文件"命運-頻次排序.txt" 字符包括中文、英文、標(biāo)點等,但不包括空格和回車 輸出格式要求: (1)字符與頻次之間采用冒號 :分隔 (2)一個字符一行,比如 理:224 斯:120 衛(wèi):100

答:

基礎(chǔ)題:

809*x=800*x+9*x+1 其中 x 代表的兩位數(shù), 8*x 的結(jié)果為兩位數(shù), 9*x 的結(jié)果為 3 位數(shù)。求 x ,及計算 809*x 的結(jié)果。

方法1:

for x in range(10, 100): if (10 <= 8*x < +100) and (100 <= 9*x <= 1000): print(x) print(809*x)

?

方法2:

a = 809 for i in range(10, 100): b = a * i + 1 if 1000 <= b <= 10000 and 8 * i < 100 and 9 * i > 99: print(i) print(b)

提高題:

對文件"命運.txt"進(jìn)行字符頻次統(tǒng)計,并將所有字符按照頻次高低排序,將排序后的字符及其頻次輸出到文件"命運-頻次排序.txt" 字符包括中文、英文、標(biāo)點等,但不包括空格和回車 輸出格式要求: (1)字符與頻次之間采用冒號 :分隔 (2)一個字符一行,比如 理:224 斯:120 衛(wèi):100


方法1:

txt = open('命運.txt', 'r', encoding='utf-8').read() txt = txt.replace('\n', '') count = {} for word in txt: count[word] = count.get(word, 0) + 1 counts = sorted(count.items(), key=lambda x: x[1], reverse=True) for word, cnt in counts: print(f'{word} : {cnt}') file = open('命運-頻次排序.txt', 'a+', encoding='utf-8') file.write(f'{word} : {cnt}' + '\n')

?

方法2:

f = open(r'命運.txt', 'r', encoding="utf-8") m = f.read().replace('\n', '') target = {} for word in m: target[word] = target.get(word, 0) + 1 # print(target) ? target = sorted(target.items(), key=lambda x: x[1], reverse=True) ? with open('命運-頻次排序1.txt', 'w', encoding='utf8') as output: for tar, count in target: output.write('{}:{}\n'.format(tar, count))f.close()

?

?





?

轉(zhuǎn)載于:https://www.cnblogs.com/ElegantSmile/p/10989023.html

總結(jié)

以上是生活随笔為你收集整理的Python【每日一问】36的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。