题一:用字典实现集合的去重特性
1. 生成100个1~100的随机值
思路:
1. range 范围
2. random.randint(a,b)
import random
x = []
for I in range(100):
x.append(random.randint(1,100))
print(x)
2. x和y的交集
思路:
1.遍历x,并且该值必须也在y中
2.创建z的字典,并且用dict.fromkeys()添加该值。该参数必须是可迭代对象(所以可以用列表存放)
inter ={}
z = []
for each in x:
if each in y:
z.append(each)
inter = dict.fromkeys(z)
整体代码(用列表推导式,简洁)
import random
x = [random.randint(1,100) for i in range(100)]
y = [random.randint(50,100) for i in range(100)]
inter = dict.fromkeys([z for z in x if z in y])
print(list(inter.keys()))
题二:破解用哈希值加密过的密码
思路:
1.存放所有字符转的哈希值
2.然后用字典的映射特性 一一对应查找
字符串转为哈希值的方法
import hashlib
result = hashlib.md5() //参数为b字符串
bstr = bytes("字符串","utf-8") //可以利用bytes 把字符串转为 b字符串
输出 result.hexdigest
021bbc7ee20b71134d53e20206bd6feb
e10adc3949ba59abbe56e057f20f883e
655d03ed12927aada3d5bd1f90f06eb7
import hashlib
# 创建一个字典,因为要破解的是hash 转为明文 所以键为哈希值,值为对应的整数
hash_dict = {}
for i in range(1000000):
#bytes(参数需要转为字符串,"utf-8")
bstr = bytes(str(i),"utf-8")
s = hashlib.md5(bstr).hexdigest()
hash_dict[s] = i
s1 = "021bbc7ee20b71134d53e20206bd6feb"
s2 = "e10adc3949ba59abbe56e057f20f883e"
print(f"{s1}的明文值为:",{hash_dict[s1]})
print(f"{s2}的明文值为:",{hash_dict[s2]})
输出: