传递列表时出现TypeError(Python)
代码:
import itertools
import string
import hashlib
def hash_f(x):
h = hashlib.md5(x)
return int(h.hexdigest(),base=16)
value = raw_input("Enter a value: ")
oneChar = [map(''.join, itertools.product(string.ascii_lowercase, repeat=1))]
twoChar = [map(''.join, itertools.product(string.ascii_lowercase, repeat=2))]
threeChar = [map(''.join, itertools.product(string.ascii_lowercase, repeat=3))]
possibleValues = oneChar + twoChar + threeChar
hashed_list = [int(hashlib.md5(x).hexdigest(), base=16) for x in possibleValues]
for x in hashed_list:
if hash_f(value) == x:
print "MATCH"
当我尝试运行这段代码时,出现的错误是:
Traceback (most recent call last):
File "hash.py", line 18, in <module>
hashed_list = [int(hashlib.md5(x).hexdigest(), base=16) for x in possibleValues]
TypeError: must be string or buffer, not list
我在脑海中反复思考,唯一能想到的问题就是和hashlib有关的错误,但我不是在逐个检查possibleValues里的每个值吗?这样的话,应该不会出错吧?
非常感谢大家的帮助!
2 个回答
0
possibleValues 是一个包含多个列表的列表。要把它变成一个字符串的列表,你需要去掉 map 调用周围的 []
。
oneChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=1))
twoChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=2))
threeChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=3))
5
map的输出本身就是一个列表,你是不是想用:
oneChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=1))
twoChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=2))
threeChar = map(''.join, itertools.product(string.ascii_lowercase, repeat=3))