# Just how do I make use of Python's itertools.groupby ()?

I have not had the ability to locate an easy to understand description of just how to in fact make use of Python's itertools.groupby() function. What I'm attempting to do is this:

• Take a checklist - in this instance, the youngsters of an externalized lxml component
• Separate it right into teams based upon some standards
• After that later on iterate over each of these teams independently.

I've assessed the documentation, and also the examples, yet I've had problem attempting to use them past a straightforward checklist of numbers.

So, just how do I use itertools.groupby()? Exists an additional strategy I should be making use of? Reminders to excellent "requirement" analysis would certainly additionally be valued.

0
2019-05-04 21:06:30
Source Share

IMPORTANT NOTE: You need to arrange your information first.

The component I really did not get is that in the instance building and construction

groups = []
uniquekeys = []
for k, g in groupby(data, keyfunc):
groups.append(list(g))    # Store group iterator as a list
uniquekeys.append(k)


k is the existing organizing key, and also g is an iterator that you can make use of to iterate over the team specified by that organizing key. To put it simply, the groupby iterator itself returns iterators.

Below is an instance of that, making use of more clear 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 " "


This will certainly offer you the result:

A bear is a animal.
A duck is a pet.

A cactus is a plant.

A rate watercraft is a vehicle.
A college bus is a lorry.

In this instance, things is a checklist of tuples where the first thing in each tuple is the team the 2nd thing comes from.

The groupby() function takes 2 debates: (1) the information to team and also (2) the function to team it with.

Below, lambda x: x[0] informs groupby() to make use of the first thing in each tuple as the organizing key.

In the above for declaration, groupby returns 3 (key, team iterator) sets - as soon as for each and every one-of-a-kind key. You can make use of the returned iterator to iterate over each specific thing because team.

Below is a somewhat various instance with the very same information, making use of a checklist understanding:

for key, group in groupby(things, lambda x: x[0]):
listOfThings = " and ".join([thing[1] for thing in group])
print key + "s:  " + listOfThings + "."


This will certainly offer you the result:

pets: bear and also duck.
plants: cactus.
lorries: rate watercraft and also college bus.

0
2019-05-19 20:57:27
Source

Can you show us your code?

The instance on the Python docs is fairly uncomplicated :

groups = []
uniquekeys = []
for k, g in groupby(data, keyfunc):
groups.append(list(g))      # Store group iterator as a list
uniquekeys.append(k)

.

So in your instance, information is a checklist of nodes, keyfunc is where the reasoning of your standards function goes and afterwards groupby() teams the information.

You have to take care to arrange the information by the standards prior to you call groupby or it will not function. groupby method in fact simply repeats via a checklist and also whenever the key adjustments it develops a new team.

0
2019-05-08 04:31:42
Source