Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the average value after list Mapping by Python

Shulou Source: shulou.com Published: 2022-06-02 16:13:43 10月05日 Update

This article mainly introduces Python how to achieve the average value of list mapping, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Average_bydef average_by (lst, fn=lambda x: X): return sum (map (fn, lst), 0.0) / len (lst) # EXAMPLESaverage_by ([{'n': 4}, {'n': 2}, {'n': 8}, {'n': 6}], lambda x: X ['n']) # 5.0

This function is used to find the average in the list. Lambda expressions and map functions are mainly used in this code snippet. The main logic of this function is to use the lambda expression and the map function to extract the iterator consisting of the values to be calculated, and then use the sum function to calculate the sum of the list, divided by the list length.

2. Lambda expression

An expression like lambda parameters: expression creates an anonymous function. In this code snippet, the lambda expression appears in the parameter definition of the function average_by and is passed to fn as an argument. So fn works in the body of the average_by function as the function just defined in the arguments.

The lambda expression in the default parameter of the function average_by is a function that returns the input parameter directly. In the example, the anonymous function passed in to average_by returns the dictionary key value of n.

3. Map function

The map function is a high-order function built into Python. This function is interesting, and its arguments are a function and an iterable object. It returns an iterator that applies the function in the parameter to the iterable object in the parameter.

4. Other similar functions

There are some similar code snippets in 30-seconds-of-python. After understanding the average_by function, all of this is understandable.

4.1 max_bydef max_by (lst, fn): return max (map (fn, lst)) # EXAMPLESmax_by ([{'nails: 4}, {' nails: 2}, {'nails: 8}, {' nails: 6}], lambda v: v ['n']) # 84.2 min_bydef min_by (lst, fn): return min (map (fn, lst)) # EXAMPLESmin_by ([{'nails: 4}) {'nails: 2}, {' nails: 8}, {'nails: 6}], lambda v: v [' n']) # 24.3 sum_bydef sum_by (lst, fn): return sum (map (fn, lst)) # EXAMPLESsum_by ([{'nails: 4}, {' nails: 2}, {'nails: 8}, {' nails: 6}] Lambda v: v ['n']) # 20 Thank you for reading this article carefully I hope the article "how to achieve the average value of Python after list mapping" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Functions parameters expressions iterations articles code fragments averages objects interesting value action examples interest at the same time rational mean dictionary numerical more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Redmi Shulou Information Microsoft