python-字典作業
阿新 • • 發佈:2018-12-10
- 重複的單詞:此處認為單詞之間以空格為分隔符, 並且不包含,和.;
1. 使用者輸入一句英文句子;
2. 打印出每個單詞及其重複的次數;
a = input('請輸入一句英文:')
b = a.split()
dict1={}
for i in b:
if i not in dict1:
dict1[i]=1
else:
dict1[i]+=1
for k,v in dict1.items():
print(k,'---->',v)
-
數字重複統計:
1). 隨機生成1000個整數;
2). 數字的範圍[20, 100],
3). 升序輸出所有不同的數字及其每個數字重複的次數;
import random
a = []
for i in range(1000):
b = random.randint(20, 25)
a.append(b)
a1 = sorted(a)
dict1={}
for i in a1:
if i not in dict1:
dict1[i]=1
else:
dict1[i]+=1
for k,v in dict1.items():
print(k,'---->',v)