> 백엔드 개발 > 파이썬 튜토리얼 > 사전 튜토리얼

사전 튜토리얼

Patricia Arquette
풀어 주다: 2025-01-24 04:12:10
원래의
988명이 탐색했습니다.

Dictionary Tutorial

#Task 1
players = {'virat':190, 'rohit':87, 'dhoni':95, 'bumrah':100, 'sachin':78, 'Rahul':160}
#Total Score
total_score = 0
for k, v in players.items():
    total_score = total_score + v
print(total_score)
print()

#Task 2
#Highest Scorer
highest = 0 
for k, v in players.items():
    if v > highest:
        highest = v
        high_k = k
print(high_k,highest)
print()


#Task 3: 
#Frequency of each letter in a given word: 
name = "delllaptop"
d = {}
for letter in name:
    d[letter] = d.get(letter,0) + 1
print(d)
print()

#Task 4 :
#Frequency of each word in a given sentence
#str = 'a rose is a rose is a rose'
str = 'Cat is a pet , Cat is on the table and Cat is sleeping'
i = 0 
previous_space = 0
dict = {}
while i < len(str):
    if str[i] == ' ':
        dict[str[previous_space:i]] = dict.get(str[previous_space:i],0)+1
        previous_space = i+1 
    i+=1
else:
    dict[str[previous_space:len(str)]] = dict.get(str[previous_space:len(str)],0)+1
print(dict)
print()


#Task 5 :
#Sort based on values: 
players = {'virat':190, 'rohit':87, 'dhoni':95, 'bumrah': 80, 'hardik':66, 'pant':34, 'sachin':98}
sorted_dict ={}
lowest = list(players.values())[0]
lowest_key = list(players.keys())[0]
i=0
length = int(len(players))
while i<length:
    for k, v in players.items():
        if v < lowest:
            lowest = v
            lowest_key = k
    sorted_dict[lowest_key] = lowest
    players = {k:v for k, v in players.items() if k!= lowest_key}
    if players != {}:
        lowest = list(players.values())[0]
        lowest_key = list(players.keys())[0]
    i+=1
print(sorted_dict)










로그인 후 복사

위 내용은 사전 튜토리얼의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿