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

4

想象一下我有这个列表:

list = ['a','a','b','a']

我想要做类似这样的事情:
print(unique(list))

为了检索唯一的项,Python 将输出 b。我该怎么做?

你是指集合吗?还是你只想打印列表中的某个元素? - Its Fragilis
列表相关的编程内容:count() 方法。 - Prometheus
@ItsFragilis 他们有一个列表,想要返回仅出现一次的元素。 - Prometheus
2
在Python中,不要使用名称“list”作为变量名,因为它会覆盖内置名称(即列表构造函数)。同样的,也不要使用“dict”、“sum”等名称。 - Gulzar
5个回答

8

统计项目,只保留计数为1的项目:

>>> data = ['a','a','b','a']
>>> from collections import Counter
>>> [k for k,v in Counter(data).items() if v == 1]
['b']

2
使用Python的set()属性,我们可以轻松检查唯一值。将列表的值插入到一个集合中。即使插入多次,集合只会存储一个值。在通过list_set=set(list1)将所有值插入集合后,将该集合转换为列表以打印它。
更多方法请参见:https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it
创建唯一函数的示例:
# Python program to check if two
# to get unique values from list
# using set

# function to get unique values
def unique(list1):
 
    # insert the list to the set
    list_set = set(list1)
    # convert the set to the list
    unique_list = (list(list_set))
    for x in unique_list:
        print x,
 

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)


list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)

输出结果应为: 第一个列表中的唯一值是 40 10 20 30 第二个列表中的唯一值是 1 2 3 4 5

你也可以使用numpy.unique,例如:

`
#Python程序检查两个列表是否相同 #使用numpy.unique获取列表中的唯一值 import numpy as np

# function to get unique values
def unique(list1):
    x = np.array(list1)
    print(np.unique(x))
 

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)


list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)

第一个列表的唯一值为 [10 20 30 40]

第二个列表的唯一值为 [1 2 3 4 5]

更多信息请查看"https://www.geeksforgeeks.org/python-get-unique-values-list/#:~:text=Using%20set()%20property%20of,a%20list%20to%20print%20it."


1
你可以使用 collections.Counter() 来实现,例如:
from collections import Counter

my_list = ['a','a','b','a']

counts = Counter(my_list)

for element, count in counts.items():
    if count == 1:
        print(element)

在这种情况下,会打印b,除此之外不会有其他输出。

或者,如果您想将结果存储在列表中:

from collections import Counter

my_list = ['a','a','b','a']

counts = Counter(my_list)

unique_elements = [element for element, count in counts.items() if count == 1]

unique_elements 在这种情况下是 ['b']


0

最直接的方法是使用一个计数器的dict

a = ['a','a','b','a']

counts = {}
for element in a:
    counts[element] = counts.get(element, 0) + 1

for element in a:
    if counts[element] == 1:
        print(element)

输出:

b

1
这些条件可以被替换成:counts[element] = counts.get(element, 0) + 1 - S.B
@SorousHBakhtiary 我会永远这样使用它。已编辑。 - Gulzar

0
将列表中元素的出现次数计算并存储在字典中 从字典中检查只出现一次的元素 将唯一(只出现一次)的元素存储在列表中 打印列表
你可以尝试这样做:
my_list = ['a','a','b','a']
my_dict = {}
only_one = []

#Step 1
for element in my_list:
  if element not in my_dict:
    my_dict[element] = 1
  else:
    my_dict[element] += 1

#Step 2 and Step 3
for key, value in my_dict.items():
  if value == 1:
    only_one.append(key)

#Step 4
for unique in only_one:
  print(unique)

输出:

b

如果您想知道其他变量包含什么:

my_dict = {'a': 3, 'b': 1}
only_one = ['b']

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接