Python|从列表中获取唯一值
给定一个列表,以任意顺序打印所有唯一数字。
例子:
Input : 10 20 10 30 40 40
Output : 10 20 30 40
Input : 1 2 1 1 3 4 3 3 5
Output : 1 2 3 4 5
方法一:遍历列表
使用遍历,我们可以遍历列表中的每个元素并检查该元素是否已经在 unique_list 中,如果它不在那里,那么我们可以将它附加到 unique_list 中。这是使用一个 for 循环和另一个 if 语句来完成的,该语句检查值是否在唯一列表中,这等效于另一个 for 循环。
Python
# Python program to check if two
# to get unique values from list
# using traversal
# function to get unique values
def unique(list1):
# initialize a null list
unique_list = []
# traverse for all elements
for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
# print list
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)
Python
# 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)
Python3
#Python program to check if two
# to get unique values from list
# using 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)
Python3
# Python program to check if two
# to get unique values from list
# importing counter from collections
from collections import Counter
# Function to get unique values
def unique(list1):
# Print directly by using * symbol
print(*Counter(list1))
# 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)
# This code is contributed by vikkycirus
输出:
the unique values from 1st list is
10 20 30 40
the unique values from 2nd list is
1 2 3 4 5
方法2:使用集合
使用Python的 set() 属性,我们可以轻松检查唯一值。将列表的值插入到集合中。 Set 只存储一个值,即使它被插入不止一次。通过 list_set=set(list1) 插入集合中的所有值后,将此集合转换为列表以打印。
Python
# 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)
输出:
the unique values from 1st list is
40 10 20 30
the unique values from 2nd list is
1 2 3 4 5
方法 3:使用 numpy.unique
使用 Python 的 import numpy,还可以获取数组中的唯一元素。在第一步将列表转换为x=numpy.array(list)然后使用numpy.unique(x)函数从列表中获取唯一值。 numpy.unique()仅返回列表中的唯一值。
Python3
#Python program to check if two
# to get unique values from list
# using 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)
输出
the unique values from 1st list is
[10 20 30 40]
the unique values from 2nd list is
[1 2 3 4 5]
方法 #4:使用 collections.Counter()
使用Python import Counter() from collections 打印 Counter 元素的所有键,或者我们直接使用“*”符号打印。
下面是上述方法的实现。
Python3
# Python program to check if two
# to get unique values from list
# importing counter from collections
from collections import Counter
# Function to get unique values
def unique(list1):
# Print directly by using * symbol
print(*Counter(list1))
# 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)
# This code is contributed by vikkycirus
输出:
the unique values from 1st list is
10 20 30 40
the unique values from 2nd list is
1 2 3 4 5