在Python中从列表中获取唯一值

在这篇文章中,我们将了解从Python列表中获取唯一值的三种方法。在处理大量原始数据时,我们经常会遇到需要从原始输入数据集中提取唯一且非重复的数据集的情况。

下面列出的方法将帮助你解决这个问题。我们马上开始吧!


如何从Python列表中获取唯一值的方法

可以使用以下任何一种方法来从 Python 列表中获取唯一值:

  • Python set() method
  • Using Python list.append() method along with a for loop
  • Using Python numpy.unique() method

使用Python的Set()函数从列表中获取唯一值。

在我们之前关于Python Set的教程中,我们知道Set将重复的值存储为一个副本。Set的这个特性可以用于在Python中从列表中获取唯一值。

  • Initially, we will need to convert the input list to set using the set() function.

句法:语法结构和规则的系统化使用。

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:

语法:

list(set-name)
  • Finally, print the new list

For breakfast, I usually have a bowl of cereal with milk and a cup of coffee.

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) 

产出:

The unique elements of the input list using set():

25
75
100
20
12

2. Python 中的列表.append() 方法和 for 循环

为了找到独特元素,我们可以使用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.

语法:

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: 请把这段话用中文进行同义转述。

Original sentence: “I am going to the store to buy some groceries.”
Paraphrased sentence: “我要去商店买一些杂货。”

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) 
      

产出:

Unique elements of the list using append():

100
75
20
12
25

3. 使用Python的numpy.unique()函数创建一个包含唯一项的列表

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:

语法:

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.

语法:
句法:

numpy.unique(numpy-array-name)

English: I want to go to the bookstore to buy a new book.
Chinese: 我想去书店买一本新书。

English: She enjoys playing tennis with her friends.
Chinese: 她喜欢和朋友一起打网球。

English: We need to clean the house before our guests arrive.
Chinese: 在客人到来之前,我们需要打扫房子。

English: He is studying for his exams at the library.
Chinese: 他正在图书馆为考试而学习。

English: The cat is sleeping on the bed.
Chinese: 猫正在床上睡觉。

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)
      

输出结果:

Unique elements of the list using numpy.unique():

[12  20  25  75 100]

总之

在这篇文章中,我们揭示了从Python列表中一组数据项中获取唯一值的各种方式。


参考资料

  • NumPy function: numpy.unique() – SciPy documentation
发表回复 0

Your email address will not be published. Required fields are marked *


广告
将在 10 秒后关闭
bannerAds