http://blog.csdn.net/cunchi4221/article/details/107478606
python 獲取唯一值
In this article, we will be understanding 3 ways to get unique values from a Python list. While dealing with a huge amount of raw data, we often come across situations wherein we need to fetch out the unique and non-repeated set of data from the raw input data-set.
在本文中,我們將了解從Python列表中獲取唯一值的3種方法 。 在處理大量原始數據時,我們經常會遇到需要從原始輸入數據集中獲取唯一且未重復的數據集的情況。
The methods listed below will help you solve this problem. Let’s get right into it!
下面列出的方法將幫助您解決此問題。 讓我們開始吧!
從Python列表中獲取唯一值的方法 (Ways to Get Unique Values from a List in Python)
Either of the following ways can be used to get unique values from a list in Python:
- Python set() method
- Using Python list.append() method along with a for loop
- Using Python numpy.unique() method
1. Python Set()從列表中獲取唯一值 (1. Python Set() to Get Unique Values from a List)
As seen in our previous tutorial on Python Set, we know that Set stores a single copy of the duplicate values into it. This property of set can be used to get unique values from a list in Python.
如我們之前關於Python Set的教程中所見,我們知道Set將重復值的單個副本存儲到其中。 set的此屬性可用於從Python列表中獲取唯一值。
- Initially, we will need to convert the input list to set using the set() function.
Syntax:
句法:
-
-
set( input_list_name)
- As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it.
- Then, we will have to convert the set back to the list using the below command/statement:
Syntax:
句法:
-
-
list( set-name)
- Finally, print the new list
Example:
例:
-
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
set_res = set(list_inp)
-
print("The unique elements of the input list using set():\n")
-
list_res = (list(set_res))
-
-
for item in list_res:
-
print(item)
Output:
輸出:
-
-
The unique elements of the input list using set():
-
-
25
-
75
-
100
-
20
-
12
2. Python list.append()和for循環 (2. Python list.append() and for loop)
In order to find the unique elements, we can apply a Python for loop along with list.append() function to achieve the same.
為了找到唯一的元素,我們可以將Python for循環與list.append()函數一起使用以實現相同目的。
- At first, we create a new (empty) list i.e res_list.
- After this, using a for loop we check for the presence of a particular element in the new list created (res_list). If the element is not present, it gets added to the new list using the append() method.
Syntax:
句法:
-
-
list. append(value)
- In case, we come across an element while traversing that already exists in the new list i.e. a duplicate element, in such case it is neglected by the for loop. We will use the if statement to check whether it’s a unique element or a duplicate one.
Example:
例:
-
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
res_list = []
-
-
for item in list_inp:
-
if item not in res_list:
-
res_list.append(item)
-
-
print("Unique elements of the list using append():\n")
-
for item in res_list:
-
print(item)
-
Output:
輸出:
-
-
Unique elements of the list using append():
-
-
100
-
75
-
20
-
12
-
25
3. Python numpy.unique()函數創建包含唯一項的列表 (3. Python numpy.unique() function To Create a List with Unique Items)
Python NumPy module has a built-in function named, numpy.unique to fetch unique data items from a numpy array.
Python NumPy模塊具有一個名為numpy.unique的內置函數,用於從numpy數組中獲取唯一的數據項。
- In order to get unique elements from a Python list, we will need to convert the list to NumPy array using the below command:
Syntax:
句法:
-
-
numpy. array(list-name)
- Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array
- and finally, we’ll print the resulting list.
Syntax:
句法:
-
-
numpy.unique(numpy- array-name)
Example:
例:
-
-
import numpy as N
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
res = N.array(list_inp)
-
unique_res = N.unique(res)
-
print("Unique elements of the list using numpy.unique():\n")
-
print(unique_res)
-
Output:
輸出:
-
-
Unique elements of the list using numpy.unique():
-
-
[ 12 20 25 75 100]
結論 (Conclusion)
In this article, we have unveiled various ways to fetch the unique values from a set of data items in a Python list.
在本文中,我們揭示了從Python列表中的一組數據項中獲取唯一值的各種方法。