R
Roman Bertle
Hello,
there is an example how to use groupby in the itertools documentation
(http://docs.python.org/lib/itertools-example.html):
# Show a dictionary sorted and grouped by value.... print k, map(itemgetter(0), g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']
Now i wonder why itemgetter is used in this example. More
straightforward is:
.... print k, list(g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']
This code does not need the operator module, and its also faster (tested
using timeit). Why was the, imho, more complicated version used as
example in the documentation?
Regards, Roman
there is an example how to use groupby in the itertools documentation
(http://docs.python.org/lib/itertools-example.html):
# Show a dictionary sorted and grouped by value.... print k, map(itemgetter(0), g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']
Now i wonder why itemgetter is used in this example. More
straightforward is:
.... print k, list(g)
....
1 ['a', 'c', 'e']
2 ['b', 'd', 'f']
3 ['g']
This code does not need the operator module, and its also faster (tested
using timeit). Why was the, imho, more complicated version used as
example in the documentation?
Regards, Roman