python 获取惟一值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种方法 。 在处理大量原始数据时,咱们常常会遇到须要从原始输入数据集中获取惟一且未重复的数据集的状况。app
The methods listed below will help you solve this problem. Let’s get right into it!ide
下面列出的方法将帮助您解决此问题。 让咱们开始吧!函数
从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:oop
如下两种方法都可用于从Python列表中获取惟一值:fetch
- Python set() method
Python set()方法
- Using Python list.append() method along with a for loop
使用Python list.append()方法和for循环
- Using Python numpy.unique() method
使用Python numpy.unique()方法
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.ui
如咱们以前关于Python Set的教程中所见,咱们知道Set将重复值的单个副本存储到其中。 set的此属性可用于从Python列表中获取惟一值。this
- Initially, we will need to convert the input list to set using the set() function.
最初,咱们须要使用set()函数将输入列表转换为set。
Syntax:教程
句法:
-
-
- As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it.
当列表转换为set时,全部重复元素的仅一个副本将放入其中。
- Then, we will have to convert the set back to the list using the below command/statement:
而后,咱们将必须使用如下命令/语句将集合转换回列表:
Syntax:
句法:
-
-
- Finally, print the new list
最后,打印新列表
Example:
例:
-
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
-
print("The unique elements of the input list using set():\n")
-
list_res = (list(set_res))
-
-
-
Output:
输出:
-
-
The unique elements of the input list using set():
-
-
-
-
-
-
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.
首先,咱们建立一个新的(空)列表,即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.
此后,使用for循环,咱们检查所建立的新列表(res_list)中是否存在特定元素。 若是不存在该元素,则使用append()方法将其添加到新列表中。
Syntax:
句法:
-
-
- 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.
若是咱们在遍历时遇到了一个在新列表中已经存在的元素,即重复元素,在这种状况下,它被for循环所忽略。 咱们将使用if语句检查它是惟一元素仍是重复元素。
Example:
例:
-
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
-
-
-
-
-
-
print("Unique elements of the list using append():\n")
-
-
-
Output:
输出:
-
-
Unique elements of the list using append():
-
-
-
-
-
-
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:
为了从Python列表中获取惟一元素,咱们须要使用如下命令将列表转换为NumPy数组:
Syntax:
句法:
-
-
- Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array
接下来,咱们将使用numpy.unique()方法从numpy数组中获取惟一数据项
- and finally, we’ll print the resulting list.
最后,咱们将打印结果列表。
Syntax:
句法:
-
-
numpy.unique(numpy-array-name)
Example:
例:
-
-
-
list_inp = [100, 75, 100, 20, 75, 12, 75, 25]
-
-
-
unique_res = N.unique(res)
-
print("Unique elements of the list using numpy.unique():\n")
-
-
Output:
输出:
-
-
Unique elements of the list using numpy.unique():
-
-
结论 (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列表中的一组数据项中获取惟一值的各类方法。