Python 查找元组中的唯一元素
在本文中,我们将学习一个Python程序来查找元组中的唯一元素。
使用的方法
以下是完成此任务的各种方法:
- 使用for循环和append()函数
-
使用collections.Counter()函数
-
使用set()函数
元组 是一种不可变的、无序的数据类型,用于在Python中存储集合。它可以在其中存储 多个值 。列表和元组在许多方面相似,但列表的长度是可变的,与元组相比,元组的长度是固定的且不可变的。
方法1:使用for循环和append()函数
步骤
执行所需任务的算法/步骤如下:
- 创建一个函数 (uniqueElements) ,该函数返回传递给它的元组中的唯一元素。
-
为存储元组的结果唯一元素创建一个新的空列表。
-
使用 for循环 遍历元组中的每个元素。
-
使用 if条件 语句以及not in运算符来检查上述定义的新列表中是否不存在相应的元组元素。
-
如果条件为真,则使用 append()函数 (在末尾向列表添加元素)将元素添加到新列表中。
-
使用 tuple()函数 将列表转换为元组。
-
返回结果元组。
-
创建一个变量来存储输入的元组。
-
通过将输入元组传递给上述定义的 uniqueElements 函数来调用该函数,并打印结果元组。
示例
以下程序使用for循环和append()函数返回输入元组中的唯一元素-
# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
# creating a new list for storing unique elements
newList = []
# traversing through each element in a tuple
for k in inputTuple:
# appending that corresponding tuple element to the new list
# if it is not present in it(i.e, storing all unique elements)
if k not in newList:
newList.append(k)
# converting the list into a tuple
resultTuple = tuple(newList)
# returning the resultant tuple
return resultTuple
# input tuple
inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
输出
在执行后,上述程序将生成以下输出 –
The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)
方法2:使用collections.Counter()函数
Counter() 函数(一个计算可哈希对象的子类,当调用/调用时会隐式地创建一个可迭代对象的哈希表)
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字从collections模块导入 Counter 函数。
-
创建一个函数 (uniqueElements) ,该函数将作为参数传递给它的元组中的唯一元素返回。
-
将给定的元组作为参数传递给 Counter() 函数,该函数将所有唯一的元组元素以及它们的频率存储为一个字典。
-
使用 keys()和tuple()函数 将上述频率字典的键(唯一的元组元素)作为元组返回。
示例
下面的程序使用collections模块的Counter函数返回输入元组中的唯一元素-
# importing Counter from the collections module
from collections import Counter
# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
#Getting all the unique tuple elements along with their frequencies
frequency = Counter(inputTuple)
# Returning all the unique tuple elements using the keys() function
return tuple(frequency.keys())
# input tuple
inputTuple = (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
输出
在执行时,上述程序将生成以下输出:
The given Tuple is: (5, 1, 8, 7, 7, 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (5, 1, 8, 7, 3, 6)
方法3:使用set()函数
set() 函数- 创建一个集合对象。由于集合中的项是无序的,所以集合中的项的顺序是随机的。它会移除所有的重复项。
步骤
以下是执行所需任务的算法/步骤。
- 创建一个函数 (uniqueElements) ,它接受一个元组作为参数,并返回这个元组中的唯一元素。
-
使用set()函数返回元组中的所有唯一元素,并使用 tuple() 函数将其转换为一个元组(创建一个元组)。
示例
以下程序使用set()函数返回输入元组中的唯一元素。
# function that returns the unique elements in a tuple passed to it
def uniqueElements(inputTuple):
# Getting all the unique elements of the tuple using the set() function
resultUnique = set(inputTuple)
# returning unique elements as a tuple using the tuple() function
return tuple(resultUnique)
# input tuple
inputTuple = (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6)
# Printing input tuple
print("The given Tuple is:", inputTuple)
#calling the above-defined uniqueElements function by passing input tuple to it
print("The Unique elements of the tuple are:", uniqueElements(inputTuple))
输出
执行时,上述程序将生成以下输出结果 –
The given Tuple is: (5, 1, 8, 'tutorialspoint', 7, 7, 'tutorialspoint', 3, 3, 6, 1, 6)
The Unique elements of the tuple are: (1, 3, 5, 6, 7, 8, 'tutorialspoint')
结论
本文介绍了三种方法来查找元组中的唯一元素。我们还学习了如何使用Counter()函数来查找任何可迭代对象(如列表、元组等)中的频率和唯一元素。Counter()方法是三种方法中最高效的,因为它使用字典来存储频率。