Home > Backend Development > Python Tutorial > How Python uses itertools.groupby() to group records according to fields

How Python uses itertools.groupby() to group records according to fields

不言
Release: 2018-10-22 17:21:57
forward
2664 people have browsed it
The content of this article is about how Python uses itertools.groupby() to group records according to fields. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Requirements

There is a series of dictionaries or object instances, and we want to group and iterate the data according to a specific field.

2. Solution

itertools.groupby() function is particularly useful when grouping data.

Example:

from operator import itemgetter
from itertools import groupby

rows=[
    {'name':'mark','age':18,'uid':'110'},
    {'name':'miaomiao','age':28,'uid':'160'},
    {'name':'miaomiao2','age':28,'uid':'150'},
    {'name':'xiaohei','age':38,'uid':'130'},
]

#首先根据age排序
rows.sort(key=itemgetter('age'))

for age,items in groupby(rows,key=itemgetter('age')):
    print(age)
    for i in items:
        print(i)
Copy after login

Result:

18
{'name': 'mark', 'age': 18, 'uid': '110'}
28
{'name': 'miaomiao', 'age': 28, 'uid': '160'}
{'name': 'miaomiao2', 'age': 28, 'uid': '150'}
38
{'name': 'xiaohei', 'age': 38, 'uid': '130'}
Copy after login

3. Analysis

Python implementation of one-key multi-value dictionary implementation

The function groupby() scans the sequence to find sequence items with the same value (or the value returned by the function specified by the parameter key) and groups them. groupby() creates an iterator, and each iteration returns a value and a sub_iterator. This iterator can produce all items with that value in the group.

What is important here is to sort the data based on age first. Because groupby() does not sort.

If you simply group the data together based on date and put it into a large data structure to allow random access, then it may be better to use defaultdict() to build a one-key multi-value dictionary:

from collections import defaultdict

rows=[
    {'name':'mark','age':18,'uid':'110'},
    {'name':'miaomiao','age':28,'uid':'160'},
    {'name':'miaomiao2','age':28,'uid':'150'},
    {'name':'xiaohei','age':38,'uid':'130'},
]

rows_by_age=defaultdict(list)
for row in rows:
    rows_by_age[row['age']].append(row)
for a in rows_by_age[28]:
    print(a)
Copy after login

Result:

{'name': 'miaomiao', 'age': 28, 'uid': '160'}
{'name': 'miaomiao2', 'age': 28, 'uid': '150'}
Copy after login

If sorting is not considered, the defaultdict method is generally faster than groupby.

The above is the detailed content of How Python uses itertools.groupby() to group records according to fields. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template