The issue here is the way you return your data. Consider a simple example:
In [263]: def foo(data):
...: for k, v in sorted(data.items()):
...: return [(k, ) + v]
...:
In [264]: foo({'a' : ('b', 'c'), 'd' : ('e', 'f')})
Out[264]: [('a', 'b', 'c')]
What's happening is that the return
statement returns the first item back to the caller. Once the function returns, it does not resume execution and return any more items as you might expect. Because of this, you only see one item returned.
There are two possibilities as a solution. You could either return everything in a list
, or use the yield
syntax.
Option 1
return <list>
In [271]: def foo(data):
...: return[(k,) + v for k, v in sorted(data.items())]
...:
In [272]: foo({'a' : ('b', 'c'), 'd' : ('e', 'f')})
Out[272]: [('a', 'b', 'c'), ('d', 'e', 'f')]
Option 2
yield
In [269]: def foo(data):
...: for k, v in sorted(data.items()):
...: yield (k, ) + v
...:
In [270]: list(foo({'a' : ('b', 'c'), 'd' : ('e', 'f')}))
Out[270]: [('a', 'b', 'c'), ('d', 'e', 'f')]
Note that list(...)
is needed around the function call because yield
results in a generator being returned, which you must iterate over to get your final list result.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…