博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 获取唯一值_从Python列表中获取唯一值
阅读量:2532 次
发布时间:2019-05-11

本文共 4273 字,大约阅读时间需要 14 分钟。

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

以下两种方法均可用于从获取唯一值

  • 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 , 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.

如我们之前关于教程中所见,我们知道Set将重复值的单个副本存储到其中。 set的此属性可用于从Python列表中获取唯一值。

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

    最初,我们需要使用set()函数将输入列表转换为set。

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.

    当列表转换为set时,所有重复元素的仅一个副本将放入其中。
  • 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():25751002012


2. Python list.append()和for循环 (2. Python list.append() and for loop)

In order to find the unique elements, we can apply a along with to achieve the same.

为了找到唯一的元素,我们可以将与一起使用以实现相同目的。

  • 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:

句法:

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 . We will use the 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():10075201225

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:

句法:

numpy.array(list-name)
  • 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:

例:

import numpy as Nlist_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列表中的一组数据项中获取唯一值的各种方法。



参考资料 (References)

翻译自:

python 获取唯一值

转载地址:http://voqzd.baihongyu.com/

你可能感兴趣的文章
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
ActiveMQ学习笔记(1)----初识ActiveMQ
查看>>
Java与算法之(2) - 快速排序
查看>>