Get the App
SLTechnology News&Howtos  ›  Development  › 

How to filter out the unique values in the list by Python

Shulou Source: shulou.com Published: 2022-06-02 16:11:20 10月21日 Update

Editor to share with you Python how to filter out the only value in the list, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Filter_uniquefrom collections import Counterdef filter_unique (lst): return [item for item, count in Counter (lst). Items () if count > 1] # EXAMPLESfilter_unique ([1,2,2,3,4,4,5]) # [2,4]

The function uses the collections.Counter function to count the list, filtering out non-unique values through list deduction (filtering out values whose count is greater than 1).

2. Collections.Counterclass 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. It can receive an iterable object and count its elements.

Its member function items () returns a new view of the key-value pair of a dictionary. Combined with collections.Counter, Counter (lst). Items () in the above code converts a list into (element, count) pairs.

3. List derivation

List derivation provides an easier way to create lists. A common use is to apply an operation to each element of a sequence or iterable object, and then use its result to create a list, or to create a subsequence by satisfying certain conditional elements.

The structure of a list derivation is the following contained in a square bracket: an expression followed by a for clause followed by zero or more for or if clauses. The result will be a new list that evaluates the expression based on the contents of the following for and if clauses.

The above filter_unique can be written as equivalent:

From collections import Counterdef filter_unique (lst): temp_list = [] for item, count in Counter (lst). Items (): if count > 1: temp_list.append (item) return temp_list# EXAMPLESfilter_unique ([1,2,2,3,4,4,5]) # [2,4] 4, filter_non_uniquefrom collections import Counterdef filter_non_unique (lst): return [item for item Count in Counter (lst). Items () if count = = 1] # EXAMPLESfilter_non_unique ([1, 2, 2, 3, 4, 4, 5]) # [1,3,5]

Filter_non_unique is similar to the above code in that it filters out non-unique values in the list.

The above is all the content of the article "how to filter out the unique values in the list by Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Elements content functions clauses objects articles code dictionaries sequences results expressions non-uniqueness storage iteration similarity less functions multiple most subclasses Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL Shulou Tech Info Shulou Technology Microsoft