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

The Python code then implements the list grouping count

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

Share

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

This article is to share with you about the Python code and then achieve the list group count, the editor thinks it is very practical, so I share it with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

1. Count_bydef count_by (arr, fn=lambda x: X): key = {} for el in map (fn, arr): key [el] = 1 if el not in key else key [el] + 1 return key# EXAMPLESfrom math import floorcount_by ([6.1,4.2,6.3], floor) # {6: 2,4: 1} count_by (['one',' two', 'three'], len) # {3: 2,5: 1}

Count_by groups the elements in the list according to the given function and returns the number of elements in each group. This uses map () to map the values of a given list using the given function. Iterate over the mapping and increase the number of elements each time it occurs.

This function uses not in to determine whether the specified key is in the current dictionary. If not, add the key to the dictionary and set the corresponding value to 1; if so, add value to 1.

two。 Use dictionary derivation

Dictionary derivation takes the form of {key_expr: value_expr for value in collection if condition}. The dictionary-derived value_expr in the group_by function is a list that is generated using list deduction. That is,

{key_expr: [x for x in collection2 if condition2] for value in collection1 if condition1}

At the same time, we can see that according to the dictionary deduction in the group_by code, it is possible to calculate the same entries in key, and according to the rules for the type of dictionaries in Pyrhon, the same key only retains the latest key-value pairs. In fact, when the key is the same, the value is the same. [el for el in lst if fn (el) = = key] there is only one variable key in the derived for statement.

> > d = {'one': 1,' two': 2, 'three': 3,' two': 2} > d {'one': 1,' two': 2, 'three': 3} > d = {' one': 1, 'two': 2,' three': 3, 'two': 22} > d {' one': 1, 'two': 22,' three': 3} >

You can use the same method here to get the list length directly after grouping. However, this method of writing traverses the list twice, which makes the program less efficient.

Def count_by (lst, fn): return {key: len ([el for el in lst if fn (el) = = key]) for key in map (fn, lst)} 3. Use collections.defaultdict to simplify the code class collections.defaultdict ([default_factory [,...]])

Collections.defaultdict contains a default_factory attribute that can be used to quickly construct a dictionary of specified styles.

When using int as default_factory, you can use defaultdict for counting. So you can use it directly to simplify the code. Compared to the dictionary-derived approach, you only need to loop through the list once.

From collections import defaultdictdef count_by (lst, fn): d = defaultdict (int) for el in lst: d [FN (el)] + = 1 return d

When using list as the default_factory, it is easy to convert a sequence (of key-value pairs) into a (key-list) dictionary.

Def group_by (lst, fn): d = defaultdict (list) for el in lst: d [FN (el)] .append (el) return d# EXAMPLESfrom math import floorgroup_by ([6.1,4.2,6.3], floor) # {4: [4.1,6.3]} group_by (['one',' two', 'three'], len) # {3: [one',' two'] ['three']} above is the Python code and then implements the list grouping count The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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