minte9
LearnRemember



pag. 123-124


Groupby

  
""" itertools.groupby
    Makes an iterator that returns consecutive keys and groups from the iterable.
    The iterable needs to already be sorted on the same key function.
"""

import itertools

DEBUG = 1

transactions = [
    {'date': '2024-08-01', 'amount': 100},
    {'date': '2024-08-02', 'amount': 150},
    {'date': '2024-08-01', 'amount': 200},
    {'date': '2024-08-03', 'amount': 50},
]

# Sort by date
transactions.sort(key=lambda x: x['date'])

# Group by date
grouped = itertools.groupby(transactions, key=lambda x: x['date'])  # Look here

# Output
for date, group in grouped:     # grouped is an iterator                                       
    print(date, list(group))    # group is a generator


if DEBUG:
    print("\nDEBUG / Without itertools.groupby:")

    def groupby(items, sortby):
        grouped = {}
        
        for t in items:
            date = t['date']
            if date not in grouped:
                grouped[date] = []
            grouped[date].append(t['amount'])  # Look here
        return grouped

    grouped = groupby(transactions, 'date')

    # Output
    for date in grouped:  # grouped is a dictionary                                       
        print(date, grouped[date])

"""
    2024-08-01 [{'date': '2024-08-01', 'amount': 100}, {'date': '2024-08-01', 'amount': 200}]
    2024-08-02 [{'date': '2024-08-02', 'amount': 150}]
    2024-08-03 [{'date': '2024-08-03', 'amount': 50}]

    DEBUG / Without itertools.groupby:
    2024-08-01 [100, 200]
    2024-08-02 [150]
    2024-08-03 [50]
"""

Compress

 
""" itertools.compress
    Makes an interator that returns elements where the corresponding item is true
    As an example, lets filter based on a Boolean list
"""

import itertools

DEBUG = 1

products = ['apple', 'banana', 'cherry', 'orange']
in_stock = [True, False, True, False]

available_products = itertools.compress(products, in_stock)  # iterator
print(list(available_products))


if DEBUG:
    print("\nDEBUG / Without itertools.compress:")

    available_products = []
    for item, available in zip(products, in_stock):
        if available:
            available_products.append(item)

"""
    ['apple', 'cherry']

    DEBUG / Without itertools.compress:
    ['apple', 'cherry']
"""

Accumulate

 
""" Accumulate sums
    Make an iterator that returns accumulated sums  from other binary functions.
"""

import itertools

N = [1, 2, 3, 4, 5]
T = []

# without itertools
total = 0
for n in N:
    total += n
    T.append(total)
print(T)

# itertools
T = itertools.accumulate(N)
print(list(T))

"""
    [1, 3, 6, 10, 15]
    [1, 3, 6, 10, 15]
"""

Count

 
""" Count
    Make an iterator that returns evenly spaced values beginning with start.
"""

import itertools

# without itertools
def custom_count(start=0, step=1):
    n = start
    while True:
        yield n
        n += step
counter = custom_count(10, 1)
first_five = [next(counter) for _ in range(5)]
print(first_five)

# itertools
counter = itertools.count(2.5, 0.5)
first_five = [next(counter) for _ in range(5)]
print(first_five)

"""
    [10, 11, 12, 13, 14]
    [2.5, 3.0, 3.5, 4.0, 4.5]
"""

Permutations

 
""" Permutations
    Return successive r length permutations of elements from the iterable.
    permutations('ABCD', 2) → AB AC AD BA BC BD CA CB CD DA DB DC
    permutations(range(3)) → 012 021 102 120 201 210
"""

import itertools

iter = itertools.permutations('ABCD', 2)
print([a + b for a, b in list(iter)])

iter = itertools.permutations(range(3))
print([f"{x}{y}{z}" for x, y, z in list(iter)])

"""
    ['AB', 'AC', 'AD', 'BA', 'BC', 'BD', 'CA', 'CB', 'CD', 'DA', 'DB', 'DC']
    ['012', '021', '102', '120', '201', '210']
"""



  Last update: 65 days ago