I am getting the error message
badfilesdic = {k: v for k, v in badfilelist} ValueError: need more than 1 value to unpack
I am not sure how to fix it!
this is the code:
def badfiles(hasheddic, filesavedin ):
print hasheddic
print '\n'
print filesavedin
badfilelist = [s.split(' : ') for s in hasheddic]
badcontentlist = [s.split(' : ') for s in filesavedin]
badfilesdic = {k: v for k, v in badfilelist}
badcontentdic = {k: v for v, k in badcontentlist}
match = ""
for hashval, filename in badcontentdic.iteritems():
if filename in badfilesdic:
match += (hashval + " File Extension: " + badfilelist[filename]) + "\n"
return match
You need to correct your code:
badfilelist = [s.split(':') for s in hasheddic]
then
badfilesdic = dict(badfilelist) # if you want to have a unique dict
or
badfilesdic = [{k:v} for k, v in badfilelist] # if you want to have a list of dicts
of maybe:
badfilesdic = tuple({k:v} for k, v in badfilelist) # if you want to have a tuple of dicts
or whatever you like. All you need is to unpack properly your variables.
The unpacking you are doing does not work as you expect. When you say k: v for k, v in hasheddic
you are declaring a tuple of two elements (k,v)
out of each element in hasheddic
. If any of the strings exceed length 2, you get the unpacking error.
For example:
s = "hi hi".split()
for a, b in s:
print a
print b
Returns
h
i
h
i
If the string was, for example "foo bar", you would get the unpacking error for both "foo" and "bar".
If you are certain that your input string is well-formatted ( split
always returns 2-element lists), you could do this instead:
{x[0]: x[1] for x in badfilelist}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.