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列表中获取唯一值:

  • Python set() methodPython 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.

如我们之前关于Python Set的教程中所见,我们知道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():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.首先,我们创建一个新的(空)列表,即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 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] 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:为了从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 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列表中的一组数据项中获取唯一值的各种方法。



参考资料 (References)

  • NumPy function: numpy.unique() – SciPy documentationNumPy函数:numpy.unique()– SciPy文档

翻译自: https://www.journaldev.com/37091/get-unique-values-from-a-list-in-python

python 获取唯一值

python 获取唯一值_从Python列表中获取唯一值相关推荐

  1. jquery 获取同级元素_如何在jQuery中获取元素的同级

    jquery 获取同级元素 In this post, we will discuss how to get the siblings of an HTML element in jQuery. jQ ...

  2. 从python中的列表中获取唯一值[重复]

    本文翻译自:Get unique values from a list in python [duplicate] This question already has an answer here: ...

  3. python权重是什么意思_在python带权重的列表中随机取值的方法

    1 random.choice python random模块的choice方法随机选择某个元素 foo = ['a', 'b', 'c', 'd', 'e'] from random import ...

  4. python从键盘输入一个列表计算输出元素的平均值_python列表查找值_在Python中查找列表平均值的5种方法...

    python列表查找值 Hi Folks! In this article, we will have a look at the various ways to find the average o ...

  5. python将大于输出列表_程序检查列表中的所有值是否都大于Python中的给定值

    在本教程中,我们将检查列表中的所有元素是否都大于数字.例如,我们有一个列表[1.2.3.4.5]和一个数字0.如果列表中的每个值都大于给定值,则返回True,否则返回False. 这是一个简单的程序. ...

  6. python 从日期列表中选出最大的_python – 从日期时间列表中获取最早和最晚时间...

    我有, timestamp=[] for x in model_obj: timestamp.append(x.start_time) print timestamp 结果: [datetime.da ...

  7. python 字典的值是列表_python实现求和python如何通过列表中字典的值对列表进行排序...

    一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1. sorted_x = sorted(x.iteritems(), key=operator.item ...

  8. Python——定义一个函数,将列表中的每个值修改为前一个值的两倍(其中,第一个值为0);

    # 定义一个函数,将列表中的每个值修改为前一个值的两倍(其中,第一个值为0): def double_number(LL):L = [i*2 for i in LL if 1 == 1]L[0] = ...

  9. python之禅 中文_《Python之禅》中对于Python编程过程中的一些建议

    <Python之禅>中对于Python编程过程中的一些建议 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  <Python之禅>中对于Pyt ...

最新文章

  1. Sql Server 2012 分页方法分析(offset and fetch)
  2. 帝国cms7.5 utf-8本地网站电脑手机模板开发同步插件即时预览修改结果
  3. mybaits十八:内置标签
  4. AI理论知识基础(19)-线性变换(1)
  5. 我为什么卸载了今日头条
  6. uva 11584——Partitioning by Palindromes
  7. 2021年中国在线旅游直播专题分析
  8. 飞鸽传书完全不知道这是什么
  9. 这两天有点热吆,star直线上涨!~Jeecg Boot
  10. jenkins插件更换源_jenkins快速入门,自动构建一个hello world项目(devops,ci/cd)
  11. 铁甲雄兵显示服务器维护,《铁甲雄兵》5月17日09:00停机维护公告
  12. 在IT界取得成功应该知道的10件事(ZT)
  13. Q96:PT(3.3):大理石纹理(Marble Texture)
  14. 单片机编程软件很简单(16),Keil单片机编程软件建立工程项目
  15. 基于python的图书管理系统设计与实现论文_图书馆管理系统的设计与实现毕业论文...
  16. python修改app定位_5种萌新技巧定位APP_SIGN代码
  17. HNOI2015解题报告
  18. linux开机自检时间长,Linux 开机自检的设置(tune2fs和fsck)
  19. Linux内核kernel panic机制浅析
  20. php电商开源框架,Sylius 开源PHP电商解决方案

热门文章

  1. 图像处理PILLOW的使用
  2. swift可选隐式可选类型
  3. Java零基础系列003——变量
  4. DevExpress GridControl双击获取行内容
  5. [转载] 民兵葛二蛋——第31集
  6. [转载] python执行shell命令的几种方法
  7. 捷信达会员管理系统SQL语句相关
  8. 说一下syslog日志吧~~~
  9. centos上自动执行脚本执行php文件
  10. 【转】高并发情况下的单例模式