Home >Backend Development >Python Tutorial >Here\'s a suitable title for your article, keeping the question format in mind: How to Group Data Pairs by Type in Python: Efficient Solutions with `defaultdict` and `itertools.groupby`

Here\'s a suitable title for your article, keeping the question format in mind: How to Group Data Pairs by Type in Python: Efficient Solutions with `defaultdict` and `itertools.groupby`

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 07:06:03889browse

Here's a suitable title for your article, keeping the question format in mind: 

How to Group Data Pairs by Type in Python: Efficient Solutions with `defaultdict` and `itertools.groupby`

Python Group By

Problem:

Given a set of data pairs where the first item is the value and the second item is the type, group them based on the type.

Example:

input = [
  ('11013331', 'KAT'),
  ('9085267', 'NOT'),
  ('5238761', 'ETH'),
  ('5349618', 'ETH'),
  ('11788544', 'NOT'),
  ('962142', 'ETH'),
  ('7795297', 'ETH'),
  ('7341464', 'ETH'),
  ('9843236', 'KAT'),
  ('5594916', 'ETH'),
  ('1550003', 'ETH'),
]

Desired Result:

result = [
  {
    'type': 'KAT',
    'items': ['11013331', '9843236']
  },
  {
    'type': 'NOT',
    'items': ['9085267', '11788544']
  },
  {
    'type': 'ETH',
    'items': ['5238761', '962142', '7795297', '7341464', '5594916', '1550003']
  },
]

Solution:

Step 1: Create a Dictionary

  • Use the collections.defaultdict to create a dictionary where keys are item types and values are lists to store values.
  • For each data pair, append the value to the list corresponding to its type.

Step 2: Convert to Expected Format

  • Convert the dictionary into the expected format by iterating over its items and creating dictionaries with 'type' and 'items' keys.

Example Code:

<code class="python">from collections import defaultdict

res = defaultdict(list)
for v, k in input:
    res[k].append(v)

output = [{'type': k, 'items': v} for k, v in res.items()]</code>

Alternative Solution using itertools.groupby:

Note: This approach requires the input to be sorted.

  • Sort the data pairs based on their types.
  • Use itertools.groupby to group data pairs by type.
  • Iterate over the grouped items to create the desired output format.

Example Code:

<code class="python">from itertools import groupby, itemgetter

sorted_input = sorted(input, key=itemgetter(1))

groups = groupby(sorted_input, key=itemgetter(1))
output = [{'type': k, 'items': [x[0] for x in v]} for k, v in groups]</code>

Note on Key Order:

  • In Python versions before 3.7, insertion order is not guaranteed for dictionaries. To keep the order consistent with the input, use collections.OrderedDict.
  • In Python 3.7 and later, dictionaries preserve insertion order by default.

The above is the detailed content of Here's a suitable title for your article, keeping the question format in mind: How to Group Data Pairs by Type in Python: Efficient Solutions with `defaultdict` and `itertools.groupby`. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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