Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the average value after list Mapping by Python

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report