本文翻译自:Get unique values from a list in python [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • Removing duplicates in lists 45 answers 删除列表中的重复项 45答案

I want to get the unique values from the following list: 我想从以下列表中获取唯一值:

[u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']

The output which I require is: 我需要的输出是:

[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']

This code works: 此代码有效:

output = []
for x in trends:if x not in output:output.append(x)
print output

is there a better solution I should use? 我应该使用更好的解决方案吗?


#1楼

参考:https://stackoom.com/question/s7CA/从python中的列表中获取唯一值-重复


#2楼

what type is your output variable? 你的输出变量是什么类型的?

Python sets are what you just need. Python 集是您所需要的。 Declare output like this: 声明输出如下:

output = set([]) # initialize an empty set

and you're ready to go adding elements with output.add(elem) and be sure they're unique. 你准备output.add(elem)添加元素,并确保它们是唯一的。

Warning: sets DO NOT preserve the original order of the list. 警告:设置不保留列表的原始顺序。


#3楼

First declare your list properly, separated by commas. 首先正确声明您的列表,用逗号分隔。 You can get the unique values by converting the list to a set. 您可以通过将列表转换为集合来获取唯一值。

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
myset = set(mylist)
print(myset)

If you use it further as a list, you should convert it back to list by doing: 如果您进一步将其用作列表,则应通过执行以下操作将其转换回列表:

mynewlist = list(myset)

Another possibility, probably faster would be to use a set from the beginning, instead of a list. 另一种可能性,可能更快,就是从头开始使用一个集合,而不是列表。 Then your code should be: 那你的代码应该是:

output = set()
for x in trends:output.add(x)
print(output)

As it has been pointed out, the sets do not maintain the original order. 正如已经指出的那样,集合不保持原始顺序。 If you need so, you should look up about the ordered set . 如果您需要,您应该查看有序集 。


#4楼

The example you provided does not correspond to lists in Python. 您提供的示例与Python中的列表不对应。 It resembles a nested dict, which is probably not what you intended. 它类似于嵌套的字典,可能不是你想要的。

A Python list: Python列表:

a = ['a', 'b', 'c', 'd', 'b']

To get unique items, just transform it into a set (which you can transform back again into a list if required): 要获取唯一项目,只需将其转换为一个集合(如果需要,您可以将其转换回列表):

b = set(a)
print b
>>> set(['a', 'b', 'c', 'd'])

#5楼

First thing, the example you gave is not a valid list. 首先,您提供的示例不是有效列表。

example_list = [u'nowplaying',u'PBS', u'PBS', u'nowplaying', u'job', u'debate',u'thenandnow']

Suppose if above is the example list. 假设上面是示例列表。 Then you can use the following recipe as give the itertools example doc that can return the unique values and preserving the order as you seem to require. 然后,您可以使用以下配方作为itertools示例文档,该文档可以返回唯一值并保留您看起来需要的顺序。 The iterable here is the example_list 这里的iterable是example_list

from itertools import ifilterfalsedef unique_everseen(iterable, key=None):"List unique elements, preserving order. Remember all elements ever seen."# unique_everseen('AAAABBBCCDAABBB') --> A B C D# unique_everseen('ABBCcAD', str.lower) --> A B C Dseen = set()seen_add = seen.addif key is None:for element in ifilterfalse(seen.__contains__, iterable):seen_add(element)yield elementelse:for element in iterable:k = key(element)if k not in seen:seen_add(k)yield element

#6楼

  1. At the begin of your code just declare your output list as empty: output=[] 在代码的开头只是将输出列表声明为空: output=[]
  2. Instead of your code you may use this code trends=list(set(trends)) 您可以使用此代码来代替您的代码trends=list(set(trends))

从python中的列表中获取唯一值[重复]相关推荐

  1. python 获取唯一值_从Python列表中获取唯一值

    python 获取唯一值 In this article, we will be understanding 3 ways to get unique values from a Python lis ...

  2. Python找出列表中出现次数最多的元素三种方式

    通过三种方式给大家介绍,具体详情如下所示: 方式一: 原理:创建一个新的空字典,用循环的方式来获取列表中的每一个元素,判断获取的元素是否存在字典中的key,如果不存在的话,将元素作为key,值为列表中 ...

  3. Python快速找到列表中所有重复的元素

    Python快速找到列表中所有重复的元素:https://blog.csdn.net/sinat_29957455/article/details/103886088 index方法 为了能够找到元素 ...

  4. Python编程对列表中字典元素进行排序的方法详解

    @本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府 本文实例讲述了Python编程对列表中字典元素进行排序的方法.分享给大家供大家参考,具体如下: 内容目录: 问题起源 对列表中的字 ...

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

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

  6. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

  7. dart - 如何从Dart中的列表中找到最小值和最大值

    dart - 如何从Dart中的列表中找到最小值和最大值? 我已经尝试了以下代码段.但是它将引发有关类型转换的错误.因为列表具有整数和 double 值的组合.但是 **list.reduce(min ...

  8. 如何从Dart中的列表中找到最小值和最大值?

    dart - 如何从Dart中的列表中找到最小值和最大值? 我已经尝试了以下代码段.但是它将引发有关类型转换的错误.因为列表具有整数和 double 值的组合.但是 **list.reduce(min ...

  9. android10唯一识别,Android 10 如何获取唯一值?

    floyda: Android 10 如何获取唯一值? 1.自己拼一个 uuid, 这个方法不是谷歌官方给予了设备唯一 ID 最佳做法(至少我在官方文档中找不到), 如果还原出厂设置, uuid 会改 ...

最新文章

  1. Android运行时候报错:android.view.InflateException: Binary XML file line #19: Binary XML file lin
  2. if(a==1且a==2且a==3),有没有可能为true?
  3. 轻量级定时任务框架:APScheduler
  4. 在 TreeView 控件中显示分层数据
  5. AdapterViewlt;?gt; arg0, View arg1, int arg2, long arg3參数含义
  6. cmd执行命令不等待返回值_[CVE20199535] Iterm2命令执行的不完整复现
  7. sklearn.preprocessing.Imputer
  8. 找出最耗资源的sql ----没明白
  9. U盘无法格式化解决方法
  10. matlab神经网络工具箱的使用
  11. navicat远程连接腾讯云主机中MySQL
  12. 吉他(guitar)
  13. 自动驾驶L1至L5智能化程度分级
  14. 通俗易懂的大数据平台概念和架构
  15. Proteus仿真——用74LS194设计一个8个灯的流水灯电路
  16. mysql联合索引案例_mysql多个联合索引的案例分析
  17. i5-10200h怎么样
  18. 微型计算机的主要硬件以及技术指标,微型计算机的硬件组成.doc
  19. 从哪里跌倒就从哪里爬起来
  20. Iverilog源码分析 -- VPI scope的实现

热门文章

  1. 内存分配策略(一):JVM栈桢及方法调用详解
  2. 算法-------寻找旋转排序数组中的最小值
  3. 【Java基础】面向对象特性
  4. HwServiceManager篇-Android10.0 HwBinder通信原理(五)
  5. Android10.0 Binder通信原理(十一)-Binder总结
  6. Android10.0 Binder通信原理(九)-AIDL Binder示例
  7. 使用 EthPM 包管理工具
  8. quartz 两个java_spring boot整合quartz实现多个定时任务的方法
  9. pcie转sata3硬盘不启动_没有地方塞硬盘?你或许需要这款扩展卡
  10. Suring开发集成部署时问题记录