Home > OS >  Calculate average of items at particular index in a python list
Calculate average of items at particular index in a python list

Time:11-03

I have a python list like this:

a = ['1111', 1, '1112', 15, '1113', 21, '2221', 1, '2222', 34, '3331', 1, '3332', 54, '3333', 65]

The strings in the odd places represent the id and step number. The first three characters are id and the last is step number. Whereas, the numbers in even indices represent duration.

Now, I want to calculate the average duration for every step number.

For example, if I have to calculate the average of the second step the list items I need to consider will be like this:

['1112', 15, '2222', 34, '3332', 54] and average will be (15 34 54) / 3. 3 because there are 3 strings which have step number 2.

Although in case of 3 there are only 2 processes. ['1113', 21, '3333', 65]. So average will be (21 65) / 2.

I have no clue right now, how to solve this. That's why I'm not including the code snippet.

P.S: I do not want to use any external library for this problem.

CodePudding user response:

You can do it like this:

a = ['1111', 1, '1112', 15, '1113', 21, '2221', 1, '2222', 34, '3331', 1, '3332', 54, '3333', 65]

def step_avg(l):
    out_dict = dict()
    l = {l[i]: l[i 1] for i in range(0, len(l), 2)}
    for string in l:
            step = int(string[3:])
            if step not in out_dict:
                    out_dict[step] = (l[string], 1) # sum_of_values, num_of_values
            else:
                    sum_of_values, num_of_values = out_dict[step]
                    out_dict[step] = (sum_of_values   l[string], num_of_values   1)
    for step in out_dict:
            sum_of_values, num_of_values = out_dict[step]
            out_dict[step] = (sum_of_values / num_of_values)
    return out_dict

print(step_avg(a))

output:

{1: 1.0, 2: 34.333333333333336, 3: 43.0}

CodePudding user response:

You can use list comprehension to filter out every second (third, etc.) step:

every_second_step = [d for id, d in zip(a[::2], a[1::2]) if id[-1] == '2']

And to find an average:

second_step_mean = sum(every_second_step) / len(every_second_step)

Same for every third step:

every_third_step = [d for id, d in zip(a[::2], a[1::2]) if id[-1] == '3']
third_step_mean = sum(every_third_step) / len(every_third_step)
  • Related