1

I am running into following error with below piece of code, basically below is what I am trying to do, below code works fine if there is only one value... if the values are more I get the error "too many values to unpack", how can I modify my code without changing the original goal...

  1. if any of the values is a key with no values remove that line from the input
  2. if any of the values is a key with values, for each value (which is key) recursively check for its values until there are none and remove the duplicate lines...a sample input and output is shown below:

    KEY    VALUES
    353311
    344670 
    332807 353314
    338169 334478
    334478 123456 34567
    123456 98670
    34567  11111  
    353314 353311
    348521 350166 350168 350169 350170 
    350166 348521
    350168 348521
    350169 348521
    350170 348521
    
    EXPECTED OUTPUT
    344670
    332807 353314 353311
    338169 334478 123456 34567 98670 11111
    348521 350166 350168 350169 350170  
    

Code:-

from collections import OrderedDict
def main ():
    with open('gerrit_dependencylist.txt') as f:
        dic = OrderedDict()
        seen = set()
        for line in f:
            #print dic,line
            spl = line.split()
            print "SPL"
            print spl
            if len(spl) == 1:
                key = spl[0]
                v = ''
            else:
                print "LINE"
                print line
                key, v = spl
            if v in dic and dic[v] == [""]:
                del dic[v]
            for k1,v1 in dic.items():
                if key in v1:
                    dic[k1].append(v)
                    break
            else:
                dic[key] = [v]

if __name__ == '__main__':
    main()

OUTPUT:-

LINE
332807 353314

LINE
338169 334478

LINE
334478 123456 34567

Traceback (most recent call last):
  File "tesst.py", line 28, in <module>
    main()
  File "tesst.py", line 18, in main
    key, v = spl
ValueError: too many values to unpack
icedwater
  • 4,701
  • 3
  • 35
  • 50
carte blanche
  • 10,796
  • 14
  • 46
  • 65

1 Answers1

2

Well, the line has 3 items

334478 123456 34567

so

spl = ["334478", "123456", "34567"]

you are trying to unpack them into two

key, v

perhaps you want

key = spl[0]
v = spl[1:]

You can also write that on one line

key, v = spl[0], spl[1:]
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • @gnibber - if I use that ..i get an error if v in dic and dic[v] == [""]: TypeError: unhashable type: 'list' – carte blanche Jun 18 '13 at 04:38
  • 1
    @user2125827, because you can't just ask a dictionary if it contains a particular value. `in dic` works for the keys. I think you mean to iterate over v to get the individual values to look up. – John La Rooy Jun 18 '13 at 04:50
  • @user2125827 Use `for value in v: if value == desired: ...` – Alex L Jun 18 '13 at 05:20
  • @user2125827 gnibbler has explained how to solve your immediate problem, if you have another then I suggest you ask a separate follow up question. – Alex L Jun 18 '13 at 05:48