用户输入一个数字,打印每一位数字及其重复的次数
1
x = '213234'
y ={}
for z in x:
y[z]=y.setdefault(z,0)+1
print(y)
2
a = '123123'
b1= {}
for i in a:
b1[i]=b1.get(i,0)+1
print(b1)
3
a = '23423425'
b = {}
for i in a:
if i not in b:
b[i]=0
b[i] +=1
print(b)
数字重复统计随机产生100个整数,数字的范围[-1000,1000],升序输出这些数字并打印其重复的次数
import random
#lastlist=[random.randint(-1000,1000) for _ in range(100)]
newlist ={}
for i in [random.randint(-1000,1000) for _ in range(100)]:
newlist[i] = newlist.get(i,0)+1
llist = sorted(newlist.items())
print(llist)
字符串重复统计,字符表’abcdefghijklmnopqrstuvwxyz’,随机挑选2个字母组成字符串,共挑选100个,降序输出所有不同的字符串及重复的次数
for i in range(97,97+26):
print(chr(i),end='')
abcdefghijklmnopqrstuvwxyz
import random
test1 = 'abcdefghijklmnopqrstuvwxyz'
lst1 =[random.choice(test1)+random.choice(test1) for _ in range(100) ]
#print(lst1)
new1={}
for w in lst1:
new1[w] = new1.get(w,0)+1
sor2= sorted(new1.items(),reverse=True)
print(sor2)
删除字典中的字符串
d = dict(a=1,b=2,c='abc')
keys=[]
for k,v in d.items():
if isinstance(v,str):
keys.append(k)
for k in keys:
d.pop(k)
print(d)