dantdj
dantdj

Reputation: 1267

TypeError when passing a list (Python)

Code:

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"

The error I get when I try to run this code is the following:

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

Going through this in my head, the only problem that I can see is an error with hashlib, but shouldn't that be negated due to the fact that I'm cycling through each individual value in possibleValues?

All help greatly appreciated!

Upvotes: 2

Views: 814

Answers (2)

bikeshedder
bikeshedder

Reputation: 7487

The possibleValues is a list of lists. Remove the [] around the map calls in order to get one list of strings:

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))

Upvotes: 0

wim
wim

Reputation: 362717

The output of map is a list already, did you mean to use:

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))

Upvotes: 5

Related Questions