2021年5月28日 星期五

[ Python 常見問題 ] How do I use itertools.groupby()?

 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:
  1. groups = []  
  2. uniquekeys = []  
  3. for k, g in groupby(data, keyfunc):  
  4.    groups.append(list(g))    # Store group iterator as a list  
  5.    uniquekeys.append(k)  
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:
  1. from itertools import groupby  
  2.   
  3. things = [("animal""bear"), ("animal""duck"), ("plant""cactus"), ("vehicle""speed boat"), ("vehicle""school bus")]  
  4.   
  5. for key, group in groupby(things, lambda x: x[0]):  
  6.     for thing in group:  
  7.         print("A %s is a %s." % (thing[1], key))  
  8.     print("")  
This will give you the output:
A bear is a animal.
A duck is a animal.

A cactus is a plant.

A speed boat is a vehicle.
A school bus is a vehicle.

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:
  1. for key, group in groupby(things, lambda x: x[0]):  
  2.     listOfThings = " and ".join([thing[1for thing in group])  
  3.     print(key + "s:  " + listOfThings + ".")  
This will give you the output:
animals: bear and duck.
plants: cactus.
vehicles: speed boat and school bus.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...