Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuple (key, value) to dictionary ValueError: dictionary update sequence element #0 has length 6; 2 is required

I have a string that I regex which returns me a tuple (see results printed) and I want to create a dictionary from it (first entry of tuple is key, second is value), which according to StackOverflow posts should work like a charm. The for loop can return multiple points which each should be added to a single dict that should be returned.

Code & Error:

import re as re

n = nuke.selectedNode()
k = n.knob('scene')
script = k.toScript().replace('\n','').split('scenegraph {')

for sgrph in script:
    for m in [re.match(r".*\bname '([Point\d]+)'.*\btransform ([0-9.e+\- ]+)",sgrph)]:
        if m is not None:
            print m.groups()
            items = dict(m.groups()) #this causes the error
            print "........."
print items

Output:

# Result: ('Point1', '1.983990908e+00 0.000000000e+00 0.000000000e+00 0.000000000e+00 0.000000000e+00 1.983990908e+00 0.000000000e+00 0.000000000e+00 0.000000000e+00 0.000000000e+00 1.983990908e+00 0.000000000e+00 5.610483289e-01 6.365304199e+03 4.553408813e+02 1.000000000e+00      ')
    Traceback (most recent call last):
      File "<string>", line 11, in <module>
    ValueError: dictionary update sequence element #0 has length 6; 2 is required
like image 838
J. doe Avatar asked Dec 01 '25 06:12

J. doe


1 Answers

In my opinion, it's a good idea to be explicit. For example, you can define your dictionary before your nested for loops, unpack explicitly and add an item to your dictionary.

res = {}

for sgrph in script:
    for m in [re.match(r".*\bname '([Point\d]+)'.*\btransform ([0-9.e+\- ]+)",sgrph)]:
        if m is not None:
            key, value = m.groups()
            res[key] = value
like image 82
jpp Avatar answered Dec 04 '25 06:12

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!