Python字典练习
练习题1.(使用字典的方法) 用户输入一个数字,打印每一位数字以及重复次数。输入和打印数字,注意输出的是字符串类型:
思路1:常规思路,定义一个字典的key保存所有数字
# the_number =input('请输入你的数字').strip().lstrip("0+-")
# # Python可以用strip()函数解决input避免输入enter确认而引起的报错
# print(type(the_number),the_number)
>>> the_number = "889977"
定义一个空字典用以存放每个数字(即,所有可能的数字)默认的重复次数,初始为0次:
>>> count = {}
>>> for i in range(10):
>>> count[i] = 0
>>> print(count)
>>> type(count[0])
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
int
通过遍历字符串每一个值的方法,检查数字重复的次数,将结果存入字典。
当然如果一开始我们定义的是一个空列表,那现在就是存入列表;至于是使用列表还是字典,在这个题目中是没有什么区别的。
>>> for i in the_number:
>>> i = int(i)
>>> count[i] = count[i]+1
>>> for k,v in count.items():
>>> if v>0:
>>> print("数字{}:出现{}次".format(k,v))
>>> print("其余数字为0次")
数字7:出现2次
数字8:出现2次
数字9:出现2次
其余数字为0次
思路2:只加有的数字到字典的key
>>> count2 = {}
>>> for c in the_number:
>>> if c not in count2.keys():
>>> count2[c]=1
>>> else:
>>> count2[c]+=1
>>> # print(d)
>>> for k,v in count2.items():
>>> if v>0:
>>> print("数字{}:出现{}次".format(k,v))
>>> print("其余数字为0次")
数字8:出现2次
数字9:出现2次
数字7:出现2次
其余数字为0次
>>> dict3 = {}
>>> for c in the_number:
>>> dict3[c] =dict3.setdefault(c,0)+1
# print(d)
>>> for k,v in dict3.items():
>>> if v>0:
>>> print("数字{}:出现{}次".format(k,v))
>>> print("其余数字为0次")
数字8:出现2次
数字9:出现2次
数字7:出现2次
其余数字为0次
>>> dict4 = {}
>>> for i in the_number:
>>> dict4[i] = dict4.get(i,0)+1
>>> print(dict)
# print(d)
>>> for k,v in dict4.items():
>>> if v>0:
>>> print("数字{}:出现{}次".format(k,v))
>>> print("其余数字为0次")
<class 'dict'>
数字8:出现2次
数字9:出现2次
数字7:出现2次
其余数字为0次
练习题2. 数字重复次数统计练习:
- 随机产生100个整数
- 数字范围[-1000, 1000]
- 升序输出这些数字并打印其重复次数
>>> import random
>>> n = 100 #随机产生100个整数
>>> numbers = [None]*n
>>> for i in range(n):
>>> numbers[i] = random.randint(-1000, 1000) #数字范围[-1000, 1000]
# print(numbers)
>>> counting = {}
>>> for i in numbers:
>>> counting[i] = counting.setdefault(i,0)+1
>>> print(sorted(counting.items()))
[(-997, 1), (-970, 1), (-964, 1), (-935, 1), (-905, 1), (-829, 1), (-826, 1), (-740, 1), (-737, 1), (-723, 1), (-690, 1), (-684, 1), (-677, 1), (-658, 1), (-626, 1), (-600, 1), (-586, 1), (-575, 1), (-550, 1), (-541, 1), (-536, 1), (-465, 1), (-431, 1), (-379, 1), (-358, 1), (-352, 1), (-341, 1), (-334, 1), (-305, 1), (-292, 1), (-288, 1), (-274, 1), (-271, 1), (-263, 1), (-258, 1), (-232, 1), (-228, 1), (-203, 1), (-169, 1), (-167, 1), (-161, 1), (-141, 1), (-127, 1), (-123, 1), (-122, 1), (-113, 1), (-43, 1), (-17, 1), (1, 1), (29, 1), (59, 1), (60, 1), (66, 1), (68, 1), (84, 1), (109, 1), (220, 1), (230, 1), (301, 1), (317, 1), (325, 1), (334, 1), (339, 1), (347, 1), (357, 1), (383, 1), (389, 1), (397, 1), (413, 1), (423, 1), (438, 1), (479, 1), (496, 1), (526, 1), (538, 1), (552, 1), (566, 1), (574, 1), (586, 1), (601, 2), (614, 1), (686, 1), (694, 1), (711, 1), (715, 1), (717, 1), (778, 1), (785, 1), (849, 1), (878, 1), (881, 1), (896, 1), (912, 1), (949, 1), (962, 1), (978, 1), (992, 1), (998, 1), (999, 1)]
>>> import random
>>> count ={}
>>> sort_list=[]
>>> for i in range(-1000, 1001):
>>> count[i]=0 #生产-1000到1000的字典key,用于统计;
>>> for i in range(100):
>>> num=random.randint(-1000,1001) # - 随机产生100个整数,- 数字范围[-1000, 1000]
>>> sort_list.append(num) # 生成用于排序的列表
>>> sort_list.sort()
>>> for i in sort_list:
>>> count[i]=count[i]+1
# 如下代码解除注释,可以打印结果
# if count[i]==0:
# pass
# else:
# print(i,count[i],"次")
练习题3. 字符串重复统计
字符表"abcdefghijklmnopqrstuvwxyz"
- 随机挑选2个字母组成字符串,共挑选100个
- 降序输出所有不同的字符串,以及重复次数。
--------------------------------给每一个字母赋一个key的值
>>> import string
>>> import random
# alphabet = string.ascii_lowercase
>>> alphabet = bytes(range(97,123)).decode()
>>> alphabet
'abcdefghijklmnopqrstuvwxyz'
随机挑选2个字母:我们用随机挑选2个字典key的方法选取随机的2个字母
>>> words = []
>>> for i in range(100): #挑选100个
words.append("".join(random.sample(alphabet,k=2)))
# words.append("".join(random.choices(alphabet,k=2))) #挑选2个字母组成字符串,此法也可
# words.append(random.choice(alphabet)+random.choice(alphabet)) #挑选2个字母组成字符串,此法也可
>>> print(words)
['pm', 'wc', 'nl', 'gj', 'lv', 'wk', 'jl', 'qe', 'bc', 'ik', 'pw', 'wv', 'yf', 'zu', 'et', 'so', 'iq', 'yu', 'tz', 'im', 'uw', 'br', 'ga', 'if', 'hg', 'wp', 'nj', 'mq', 'it', 'dq', 'ny', 'lr', 'zx', 'xq', 'ka', 'lh', 'oa', 'ti', 'yx', 'je', 'oi', 'qk', 'ph', 'fp', 'wx', 'yj', 'vl', 'ke', 'wv', 'zk', 'hn', 'zk', 'jf', 'eb', 'dt', 'zb', 'uq', 'ne', 'ju', 'fj', 'sc', 'mf', 'zo', 'yn', 'ep', 'vq', 'ro', 'vl', 'rl', 'ae', 'xf', 'cu', 'eh', 'ck', 'vm', 'mq', 'yz', 'wa', 'yt', 'pg', 'hr', 'ms', 'tv', 'bf', 'mf', 'ad', 'ln', 'dk', 'dx', 'ig', 'ew', 'iv', 'ot', 'qm', 'ig', 'qz', 'ap', 'cu', 'bq', 'oh']
>>> d = {}
>>> for i in words:
>>> d[i] = d.get(i,0)+1
>>> print(d)
{'pm': 1, 'wc': 1, 'nl': 1, 'gj': 1, 'lv': 1, 'wk': 1, 'jl': 1, 'qe': 1, 'bc': 1, 'ik': 1, 'pw': 1, 'wv': 2, 'yf': 1, 'zu': 1, 'et': 1, 'so': 1, 'iq': 1, 'yu': 1, 'tz': 1, 'im': 1, 'uw': 1, 'br': 1, 'ga': 1, 'if': 1, 'hg': 1, 'wp': 1, 'nj': 1, 'mq': 2, 'it': 1, 'dq': 1, 'ny': 1, 'lr': 1, 'zx': 1, 'xq': 1, 'ka': 1, 'lh': 1, 'oa': 1, 'ti': 1, 'yx': 1, 'je': 1, 'oi': 1, 'qk': 1, 'ph': 1, 'fp': 1, 'wx': 1, 'yj': 1, 'vl': 2, 'ke': 1, 'zk': 2, 'hn': 1, 'jf': 1, 'eb': 1, 'dt': 1, 'zb': 1, 'uq': 1, 'ne': 1, 'ju': 1, 'fj': 1, 'sc': 1, 'mf': 2, 'zo': 1, 'yn': 1, 'ep': 1, 'vq': 1, 'ro': 1, 'rl': 1, 'ae': 1, 'xf': 1, 'cu': 2, 'eh': 1, 'ck': 1, 'vm': 1, 'yz': 1, 'wa': 1, 'yt': 1, 'pg': 1, 'hr': 1, 'ms': 1, 'tv': 1, 'bf': 1, 'ad': 1, 'ln': 1, 'dk': 1, 'dx': 1, 'ig': 2, 'ew': 1, 'iv': 1, 'ot': 1, 'qm': 1, 'qz': 1, 'ap': 1, 'bq': 1, 'oh': 1}
>>> print(sorted(d.items(),reverse = True)) #降序输出所有不同的字符串,以及重复次数。
[('zx', 1), ('zu', 1), ('zo', 1), ('zk', 2), ('zb', 1), ('yz', 1), ('yx', 1), ('yu', 1), ('yt', 1), ('yn', 1), ('yj', 1), ('yf', 1), ('xq', 1), ('xf', 1), ('wx', 1), ('wv', 2), ('wp', 1), ('wk', 1), ('wc', 1), ('wa', 1), ('vq', 1), ('vm', 1), ('vl', 2), ('uw', 1), ('uq', 1), ('tz', 1), ('tv', 1), ('ti', 1), ('so', 1), ('sc', 1), ('ro', 1), ('rl', 1), ('qz', 1), ('qm', 1), ('qk', 1), ('qe', 1), ('pw', 1), ('pm', 1), ('ph', 1), ('pg', 1), ('ot', 1), ('oi', 1), ('oh', 1), ('oa', 1), ('ny', 1), ('nl', 1), ('nj', 1), ('ne', 1), ('ms', 1), ('mq', 2), ('mf', 2), ('lv', 1), ('lr', 1), ('ln', 1), ('lh', 1), ('ke', 1), ('ka', 1), ('ju', 1), ('jl', 1), ('jf', 1), ('je', 1), ('iv', 1), ('it', 1), ('iq', 1), ('im', 1), ('ik', 1), ('ig', 2), ('if', 1), ('hr', 1), ('hn', 1), ('hg', 1), ('gj', 1), ('ga', 1), ('fp', 1), ('fj', 1), ('ew', 1), ('et', 1), ('ep', 1), ('eh', 1), ('eb', 1), ('dx', 1), ('dt', 1), ('dq', 1), ('dk', 1), ('cu', 2), ('ck', 1), ('br', 1), ('bq', 1), ('bf', 1), ('bc', 1), ('ap', 1), ('ae', 1), ('ad', 1)]
转载:https://blog.csdn.net/pagean/article/details/105385910
查看评论