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 find parity exception items in list by Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how Python can find the parity items in the list. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

1.find_parity_outliers

From collections import Counterdef find_parity_outliers (nums): return [x for x in nums if x% 2! = Counter ([n% 2 for n in nums]). Most_common () [0] [0]] # EXAMPLESfind_parity_outliers ([1,2,3,4,6]) # [1,3]

The find_parity_outliers function receives a list and returns parity exceptions in the list. Parity exceptions refer to items that are different in parity from most items in the list. The function uses list deduction to check whether each item in the input list is a parity exception. List deduction and remainder operations (% 2) are used in Counter to extract the parity properties of each item in its input list one by one. Use collections.Counter.most_common () to get the most common parity in the list.

2.class collections.Counter ([iterable-or-mapping])

Counter is a subclass of dict that is used to count hashable objects. It is a collection where elements are stored like dictionary keys (key) and their counts are stored as values. The count can be any integer value, including 0 and negative. Most_common ([n]) is a method provided by Counter, which

Returns a list of the n most common elements and the number of occurrences, sorted by frequency from highest to lowest. If n is omitted or is None,most_common (), all elements in the counter are returned. Elements with equal count values are sorted in the order in which they first appear.

> from collections import Counter > Counter ('abracadabra'). Most_common (3) [(' axiang, 5), ('baked, 2), (' ritual, 2)]

[0] [0] locates the elements in the result list and extracts the elements that appear most frequently.

> Counter ('abracadabra'). Most_common (3) [0] [0] 'a' on "how to find parity items in the list by Python" is the end of this article. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

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