字典练习题

数字统计

用户输入诺干个数字,打印每一位数字及重复的次数

nums = '001123220'.strip().lstrip('0')

d = {}
for i in nums:
    d[i] = d.get(i,0) + 1
print(d)

dict1 = {}

for c in nums:
    dict1[c] = dict1.setdefault(c,0) + 1
print(dict1)


nums = '001123220'.strip().lstrip('0')
counter = {}

for c in nums:
    if c not in counter.keys():
        counter[c] = 0
    counter[c] += 1

print(counter)


for c in nums:
    if c in counter.keys():
        counter[c] +=1
    else:
        counter[c] = 1
print(counter)

 使用lambda函数:

from collections import defaultdict
nums = '1123220'
counter = defaultdict(lambda:0)
for x in nums:
    counter[x] +=1
print(counter)

使用defaultdict:

from collections import defaultdict
nums = '1123220'
counter = defaultdict(int)
for x in nums:
    counter[x] +=1
print(counter)

 

数字重复统计

随机生成100个整数

数字范围[-1000,1000]

升序输出这些数字

打印这些数字重复的次数  

import  random

nums = [random.randint(-1000,1000) for i in range(100)]
print(nums)

print(sorted(nums))

dict1 = {}
for c in nums:
    dict1[c] = dict1.setdefault(c,0) + 1
print(dict1)

counter = {}
for c in nums:
if c not in counter.keys():
counter[c] = 0
counter[c] += 1

print(counter)

 

随机从26个字母中抽取两个字母,同时统计出现的次数

 生成字母表:

import string
alphabet = string.ascii_lowercase
print(alphabet)

alphabet = bytes(range(0x61,123)).decode()
print(alphabet)

 生成两个字母:

import random
chr(random.randint(97,122)) + chr(random.randint(97,122))

import random
 "".join(chr(random.randint(97,122)) for _ in range(2))

 


"".join([chr(i) for i in range(97,123)])

import
random "".join([ random.choice("".join([chr(i) for i in range(97,123)])) for j in range(10)]) import random "".join([chr(random.randint(97,123)) for _ in range(10)])

 

 

alpha = 'abcdefghijkmonpqrstuvwxyz'

# lst = []
# for i in range(100):
#     lst.append(random.choice(alpha) + random.choice(alpha))
# print(lst)

lst1 = [None] * 100
for i in range(100):
    #lst1[i] = random.choice(alpha) + random.choice(alpha) #此方式可能存在两个重复的字母例如 xx yy 
    lst1[i] = "".join(random.sample(alpha,k=2))
print(lst1)

d1 = {}

for c in lst1:
    d1[c] = d1.setdefault(c,0) + 1
print(d1)

 

列表解析式实现:

s1 = set([ chr(random.randint(97,122)) + chr(random.randint(97,122)) for i in range(100)])

alpha = 'abcdefghijkmonpqrstuvwxyz'

lst1 = ["".join(random.choice(alpha) for j in range(2)) for i in range(100)]
print(lst1)

d1 = {}

for c in lst1:
    d1[c] = d1.get(c,0) + 1
print(d1)

生成器迭代器实现:

lst1 = ("".join(random.choices(alpha,k=2)) for i in range(100)) #Python >= 3.6 
s1 = set(("".join(chr(random.randint(97,122))for j in  range(2)) for i in range(100)))
s2 = set(("".join(chr(random.randint(97,122))for j in  range(2)) for i in range(100)))
alpha = 'abcdefghijkmonpqrstuvwxyz'

lst1 = ("".join(random.sample(alpha,k=2)) for i in range(100))
print(lst1)

d1 = {}
for c in lst1:
    d1[c] = d1.get(c,0) + 1
print(d1)

<generator object <genexpr> at 0x0000025592291518>
{'fn': 1, 'pi': 1, 'vy': 1, 'qu': 1, 'zo': 1, 'ar': 1, 'wu': 1, 'vz': 1, 'mu': 1, 'yw': 1, 'jk': 1, 'ia': 1, 'pd': 1, 'jf': 1, 'ip': 1, 'xm': 3, 'vf': 1, 'ji': 1, 'hb': 1, 'vx': 1, 'qx': 1, 'hx': 1, 'kt': 1, 'th': 1, 'wn': 2, 'jd': 1, 'nu': 1, 'wv': 1, 'cp': 1, 'br': 1, 'kz': 1, 'zq': 2, 'nb': 1, 'zy': 1, 'qn': 1, 'gu': 1, 'sf': 1, 'iq': 1, 'xo': 1, 'ap': 2, 'wp': 1, 'zf': 1, 'xs': 1, 'hp': 1, 'hf': 1, 'xd': 1, 'ry': 2, 'mx': 1, 'zj': 2, 've': 1, 'ki': 1, 'fu': 2, 'ku': 1, 'oa': 1, 'hr': 2, 'rc': 1, 'sr': 1, 'sm': 1, 'ux': 1, 'vs': 1, 'wq': 1, 'jw': 1, 'za': 1, 'ih': 1, 'xv': 1, 'sw': 1, 'iz': 1, 'se': 1, 'ta': 1, 'ys': 1, 'ds': 1, 'gh': 1, 'yq': 1, 'dh': 1, 'wf': 1, 'dz': 1, 'fc': 1, 'us': 1, 'zg': 1, 'ra': 1, 'qb': 1, 'mj': 1, 'tn': 1, 'iw': 1, 'dx': 1, 'dt': 1, 'mc': 1, 'nd': 1, 'gt': 1, 'ck': 1, 'mi': 1}

 

posted @ 2020-04-06 21:15  Alrenn  阅读(291)  评论(0)    收藏  举报