日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:計算字符串中的字符頻率

創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元南山做網(wǎng)站,已為上家服務(wù),為南山各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

編寫一個 Python 程序,使用 for 循環(huán)和字典來計算字符串中的字符頻率。在這個 Python 示例中,for 循環(huán)迭代整個字符串。我們把所有的字符數(shù)分配給字典鍵。

# Count Charcaters Frequency in a String

string = input("Please enter the Your Own String = ")
chardict = {}

for num in string:
    keys = chardict.keys()
    if num in keys:
        chardict[num] += 1
    else:
        chardict[num] = 1

print(chardict)

這個 Python 程序使用 Counter 集合對給定字符串中的所有字符頻率進(jìn)行計數(shù)。

# Count Charcaters Frequency in a String

from collections import Counter

string = input("Please enter the Your Own String = ")

chars = Counter(string)

print("Total Characters Frequency in this String = ")
print(chars)
Please enter the Your Own String = hello python programmers
Total Characters Frequency in this String = 
Counter({'o': 3, 'r': 3, 'h': 2, 'e': 2, 'l': 2, ' ': 2, 'p': 2, 'm': 2, 'y': 1, 't': 1, 'n': 1, 'g': 1, 'a': 1, 's': 1})

文章題目:Python程序:計算字符串中的字符頻率
網(wǎng)頁地址:http://www.dlmjj.cn/article/dhjgjop.html