Home > Mobile >  Reducing list of tuples and generating mean values (being pythonic!)
Reducing list of tuples and generating mean values (being pythonic!)

Time:02-21

I'm a Python beginner and I have written some code which works (shown at the end) but I'd prefer to learn a pythonic way to do this.

I have a list of lists of tuples, as below. There might be anywhere from 1 to 6 tuples in each list. I'd like to determine the mean of the three numerical values in each of the lists, and end up with just one tuple in each list, so something like the second snippet.

[
    [
        ("2022-02-21 20:30:00", None, 331.0),
        ("2022-02-21 21:00:00", None, 324.0),
        ("2022-02-21 21:30:00", None, 298.0),
    ],
    [
        ("2022-02-21 22:00:00", None, 190.0),
        ("2022-02-21 22:30:00", None, 221.0),
        ("2022-02-21 23:00:00", None, 155.0),
    ],
    [
        ("2022-02-21 23:30:00", None, 125.0),
        ("2022-02-22 00:00:00", None, 95.0),
        ("2022-02-22 00:30:00", None, 69.0),
    ],
]
[
    [
        ("2022-02-21 20:30:00", None, 317.7),
    ],
    [
        ("2022-02-21 22:00:00", None, 188.7),
    ],
    [
        ("2022-02-21 23:30:00", None, 96.3),
    ],
]
for li in data:
    li = [list(t) for t in li]
    sum = 0

    for t in li:
        sum = sum   t[tuple_idx]

    mean = sum / len(li)

    li[0][tuple_idx] = mean

    new_data.append(tuple(li[0]))

data = new_data

CodePudding user response:

This is probably the most pythonic you can get with this:

from statistics import fmean

result = [
    [(*x[0][:-1], fmean(map(lambda y: y[2], x)))] for x in inputs
]

You can flatten it to a list of tuples if you want by dropping the square brackets inside the comprehension.

CodePudding user response:

I wouldn't try to make it more pythonic or shorter but more readable.

So I would keep your version with few small changes.

And I would use names which means something.

And there is function sum() so I wouldn't use it as variable.

new_data = []

value_idx = 2

for group in data:
    
    total_sum = sum(item[value_idx] for item in group) 
    mean = total_sum / len(group)
    
    first_item = list(group[0])    
    first_item[value_idx] = mean
    
    new_data.append( [tuple(first_item)] )

data = new_data
  • Related