연습의 파이썬 사전

1. 연습 1 : 디지털 반복 통계

통계 디지털 반복 :
1) 난수 발생 정수 1,000 ;.
범위 [20, 100] 2) 참조.
3) 번호의 오름차순으로 출력하고 각 숫자의 반복의 횟수의 모든 다른 ;.

import random
all_num = []

for item in range(1000):
    all_num.append(random.randint(20,100))

# 对生成好的1000个数进行排序,然后添加到字典中
sorted_num = sorted(all_num)

num_dict ={}

for num in sorted_num:
    if num in num_dict:
        num_dict[num] += 1

    else:
        num_dict[num] = 1

print(num_dict)

출력 :
그림 삽입 설명 여기

2. 운동 2 : 단어의 반복 통계

반복 된 단어 : 단어 구분 기호 사이에 공백과 포함하지 않고 여기있는 ;.
영어로 문장을 입력하는 사용자,
각 단어와 반복 횟수를 인쇄;

s = input('s:')

# 1.把每个单词分割处理
s_li = s.split()

word_dict ={}

for item in s_li:
    if item not in word_dict:
        word_dict[item] = 1
    else:
        word_dict[item] += 1
print(word_dict)

출력 :
그림 삽입 설명 여기

게시 60 개 원래 기사 · 원 찬양 6 · 전망 1368

추천

출처blog.csdn.net/weixin_45775963/article/details/103636530