Source From Here
Question
I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. I've reviewed the documentation, but I've had trouble trying to apply them beyond a simple list of numbers. So, how do I use of itertools.groupby()? Is there another technique I should be using? Pointers to good "prerequisite" reading would also be appreciated.
HowTo
The part I didn't get is that in the example construction:
k is the current grouping key, and g is an iterator that you can use to iterate over the group defined by that grouping key. In other words, the groupby iterator itself returns iterators.
Here's an example of that, using clearer variable names:
This will give you the output:
In this example, things is a list of tuples where the first item in each tuple is the group the second item belongs to.
The groupby() function takes two arguments: (1) the data to group and (2) the function to group it with.
Here, lambda x: x[0] tells groupby() to use the first item in each tuple as the grouping key.
In the above for statement, groupby returns three (key, group iterator) pairs - once for each unique key. You can use the returned iterator to iterate over each individual item in that group. Here's a slightly different example with the same data, using a list comprehension:
This will give you the output:
I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. I've reviewed the documentation, but I've had trouble trying to apply them beyond a simple list of numbers. So, how do I use of itertools.groupby()? Is there another technique I should be using? Pointers to good "prerequisite" reading would also be appreciated.
HowTo
The part I didn't get is that in the example construction:
- groups = []
- uniquekeys = []
- for k, g in groupby(data, keyfunc):
- groups.append(list(g)) # Store group iterator as a list
- uniquekeys.append(k)
Here's an example of that, using clearer variable names:
- from itertools import groupby
- things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]
- for key, group in groupby(things, lambda x: x[0]):
- for thing in group:
- print("A %s is a %s." % (thing[1], key))
- print("")
In this example, things is a list of tuples where the first item in each tuple is the group the second item belongs to.
The groupby() function takes two arguments: (1) the data to group and (2) the function to group it with.
Here, lambda x: x[0] tells groupby() to use the first item in each tuple as the grouping key.
In the above for statement, groupby returns three (key, group iterator) pairs - once for each unique key. You can use the returned iterator to iterate over each individual item in that group. Here's a slightly different example with the same data, using a list comprehension:
- for key, group in groupby(things, lambda x: x[0]):
- listOfThings = " and ".join([thing[1] for thing in group])
- print(key + "s: " + listOfThings + ".")
沒有留言:
張貼留言