Is there any pythonic way to find average of specific tuple elements in array?

前端 未结 8 1122
你的背包
你的背包 2021-01-01 11:35

I want to write this code as pythonic. My real array much bigger than this example.

( 5+10+20+3+2 ) / 5

print(np.mean(array,key=lambda x:x[1])

相关标签:
8条回答
  • 2021-01-01 12:02

    If you're open to more golf-like solutions, you can transpose your array with vanilla python, get a list of just the numbers, and calculate the mean with

    sum(zip(*array)[1])/len(array)
    
    0 讨论(0)
  • 2021-01-01 12:08

    If you are using Python 3.4 or above, you could use the statistics module:

    from statistics import mean
    
    average = mean(value[1] for value in array)
    

    Or if you're using a version of Python older than 3.4:

    average = sum(value[1] for value in array) / len(array)
    

    These solutions both use a nice feature of Python called a generator expression. The loop

    value[1] for value in array
    

    creates a new sequence in a timely and memory efficient manner. See PEP 289 -- Generator Expressions.

    If you're using Python 2, and you're summing integers, we will have integer division, which will truncate the result, e.g:

    >>> 25 / 4
    6
    
    >>> 25 / float(4)
    6.25
    

    To ensure we don't have integer division we could set the starting value of sum to be the float value 0.0. However, this also means we have to make the generator expression explicit with parentheses, otherwise it's a syntax error, and it's less pretty, as noted in the comments:

    average = sum((value[1] for value in array), 0.0) / len(array)
    

    It's probably best to use fsum from the math module which will return a float:

    from math import fsum
    
    average = fsum(value[1] for value in array) / len(array)
    
    0 讨论(0)
  • 2021-01-01 12:13

    you can use map instead of list comprehension

    sum(map(lambda x:int(x[1]), array)) / len(array)
    

    or functools.reduce (if you use Python2.X just reduce not functools.reduce)

    import functools
    functools.reduce(lambda acc, y: acc + y[1], array, 0) / len(array)
    
    0 讨论(0)
  • 2021-01-01 12:14

    Just find the average using sum and number of elements of the list.

    array = [('a', 5) , ('b', 10), ('c', 20), ('d', 3), ('e', 2)]
    avg = float(sum(value[1] for value in array)) / float(len(array))
    print(avg)
    #8.0
    
    0 讨论(0)
  • 2021-01-01 12:19

    If you do want to use numpy, cast it to a numpy.array and select the axis you want using numpy indexing:

    import numpy as np
    
    array = np.array([('a', 5) , ('b', 10), ('c', 20), ('d', 3), ('e', 2)])
    print(array[:,1].astype(float).mean())
    # 8.0
    

    The cast to a numeric type is needed because the original array contains both strings and numbers and is therefore of type object. In this case you could use float or int, it makes no difference.

    0 讨论(0)
  • 2021-01-01 12:27

    You could use map:

    np.mean(list(map(lambda x: x[1], array)))

    0 讨论(0)
提交回复
热议问题