【发布时间】:2018-10-22 18:07:48
【问题描述】:
我有一个字符串,我正则表达式返回一个元组(查看打印的结果),我想从中创建一个字典(元组的第一个条目是键,第二个是值),根据 StackOverflow 帖子应该像一种魅力。 for 循环可以返回多个点,每个点都应添加到应返回的单个 dict 中。
代码和错误:
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
输出:
# 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
【问题讨论】:
标签: python dictionary tuples