python3——字典(dict)小练习
题目要求1数字重复统计:(1).随机生成1000个整数;(2).数字的范围[20,100];(3).升序输出所有不同的数字及其每个数字重复的次数;代码示例import randomall_nums=[]for item in range(1000):all_nums.append(random.randint(20,101))sorted_nums=sorted(all_nums)num_dict
·
题目要求1
数字重复统计:
(1).随机生成1000个整数;
(2).数字的范围[20,100];
(3).升序输出所有不同的数字及其每个数字重复的次数;
代码示例
import random
all_nums=[]
for item in range(1000):
all_nums.append(random.randint(20,101))
sorted_nums=sorted(all_nums)
num_dict={}
for num in sorted_nums:
if num in num_dict:
num_dict[num]+=1
else:
num_dict[num]=1
print(num_dict)
运行结果
{20: 16, 21: 11, 22: 9, 23: 13, 24: 14, 25: 11, 26: 12, 27: 11, 28: 16, 29: 15, 30: 13, 31: 15, 32: 9,
33: 9, 34: 10, 35: 13, 36: 11, 37: 15, 38: 14, 39: 12, 40: 13, 41: 14, 42: 12, 43: 6, 44: 12, 45: 14,
46: 11, 47: 11, 48: 9, 49: 11, 50: 7, 51: 11, 52: 15, 53: 12, 54: 12, 55: 16, 56: 8, 57: 10, 58: 16,
59: 14, 60: 8, 61: 12, 62: 13, 63: 13, 64: 12, 65: 12, 66: 13, 67: 14, 68: 5, 69: 13, 70: 7, 71: 11,
72: 10, 73: 15, 74: 10, 75: 13, 76: 18, 77: 12, 78: 9, 79: 15, 80: 14, 81: 15, 82: 11, 83: 11, 84: 10,
85: 19, 86: 13, 87: 13, 88: 16, 89: 13, 90: 15, 91: 12, 92: 8, 93: 14, 94: 15, 95: 11, 96: 13, 97: 14,
98: 12, 99: 9, 100: 11, 101: 13}
Process finished with exit code 0
题目要求2
重复的单词:此处认为单词之间以空格为分隔符,并且不包括,和. ;
#1.用户输入一句英文句子;
#2.打印出每个单词及其重复的次数;
代码示例
s=input('请输入英文句子:')
s_li=s.split()
word_dict={}
for item in s_li:
if item in word_dict:
word_dict[item]+=1
else:
word_dict[item]=1
print(word_dict)
运行结果
题目要求3
1.生成100个银行卡号:
卡号以6102009开头,后面3位依次是(001,002,003,......,100)
2.生成关于银行卡号的字典,默认每个卡号的初始密码为‘Redhat’;
3.输出卡号和密码,格式如下:
卡号 密码
6102009001 0000000
代码示例
account_num=[]
for i in range(100):
account_num.append('6102009%.3d'%(i+1))
account_info={}.fromkeys(account_num,'redhat')
print('卡号\t\t\t\t\t密码')
for k,v in account_info.items():
print(k,'\t\t\t',v)
运行结果
中间部分省略。。。。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)