Type error in python!
closedset = set()
root = (5,6)
for u,v开发者_JAVA技巧 in root:
if v is not closedset:
closedset.add(root)
print closedset
Error:
for u,v in root:
TypeError: unpack non-sequence
What should i do with type of error?
root = [(5,6)]
...should work. for iterates through a list or set, returning first u, then v. If you want to return both parts of the set, you'll have to add itself to a list.
I'm not sure I understand what you're trying to do. Maybe:
roots = [(5, 6), (2, 3)]
for u, v in roots:
if f not in closed:
closed.add(v)
print closed
Note a few changes:
roots
is now a list of tuples.for u, v in roots
will correctly "unpack" each tuple intou
andv
- by
if v is not closed
your probably meantif f not in closed
, ifclosed
is a dictionary of some kind - if
close.add
is a method (of a set?), then it has to be called with parens()
and not brackets()
root = ((5, 6),)
or
u, v = root
Depending on what your intentions are.
for u,v in [root]:
print u,v
will do what you want.
精彩评论