Python 查找范围内未在集合中的数字

Python 查找范围内未在集合中的数字

在Python中,我们可以使用not运算符或减法运算符以及counter函数来查找在范围内但不在集合中的数字。

Python的 set 是一组未排序的元素。集合的元素必须是唯一的、不可改变的,并且集合必须消除任何重复项。由于集合是可变的,所以它们在创建后可以被修改。

示例

假设我们已经输入了一个 输入集和范围

输入

inputSet = {3, 10, 2, 11, 15, 4, 1}
lowIndex, highIndex = 0, 8

输出

[0, 5, 6, 7]

这里0,5,6,7是存在于给定范围中但不存在于集合中的元素。

使用for循环和not运算符

range()函数 返回一个从0开始递增1(默认)的数字序列,直到给定的数字之前停止。

not运算符 (一个逻辑运算符,如果语句/语句不为真则返回True,否则返回False)

步骤

以下是执行所需任务的算法/步骤 –

  • 创建一个变量来存储 输入的集合。

  • 打印输入的集合。

  • 为输入的 lowhigh 索引创建两个单独的变量。

  • 创建一个空列表来存储在输入集合中不存在的结果数字。

  • 使用 for循环 遍历输入低和高索引范围,使用 range() 函数。

  • 使用 if条件语句 检查当前索引是否不在输入集合中,使用 not运算符

  • 使用 append() 函数(将元素添加到列表末尾)将当前元素(索引)添加到结果列表中。

  • 打印在给定输入范围内不在集合中的结果元素列表。

示例

以下程序使用for循环和not运算符返回给定输入范围内不在输入集合中的数字列表 –

# input set
inputSet = {3, 10, 2, 11, 15, 4, 1}
# printing input set
print("Input set:", inputSet)
# input low and high indexes
lowIndex, highIndex = 0, 8
# resultant list for storing numbers, not in the set
resultantList = []
# travsersing in a range from input low and high index
for i in range(lowIndex, highIndex):
    # checking whether the current index does not exist in the input set
    if i not in inputSet:
        # appending the current element to the resultant list
        resultantList.append(i)
# printing the resultant list of elements not in a set
# within the specified range
print("Resultant list of elements not in a set within the specified range:\n", resultantList)

输出

执行上述程序后,将生成以下输出-

Input set: {1, 2, 3, 4, 10, 11, 15}
Resultant list of elements not in a set within the specified range:
 [0, 5, 6, 7]

使用“-”运算符

下面的程序使用“-”运算符返回一个给定输入范围内不在输入集合中的数字列表 –

示例

# input set 
inputSet = {3, 10, 2, 11, 15, 4, 1}
# printing input set 
print("Input set:", inputSet)
# input low and high indexes
lowIndex, highIndex = 0, 8
# Converting the numbers in the range to set 
# Subtracting this set from the given input set
resultantList = list(set(range(lowIndex, highIndex)) - inputSet)
# printing the resultant list of elements not in a set 
print("Resultant list of elements not in a set within the specified range:\n", resultantList)

输出

在执行以上程序后,将生成以下输出:

Input set: {1, 2, 3, 4, 10, 11, 15}
Resultant list of elements not in a set within the specified range:
 [0, 5, 6, 7]

使用Counter()函数

Counter()函数 是一个计数可哈希对象的子类。当调用/调用时,它隐式地创建一个可迭代的哈希表。

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字从collections模块导入Counter函数。

  • 使用 Counter() 函数将输入集合的元素频率作为键值对返回。

  • 为存储输入 lowhigh 索引的两个单独变量创建。

  • 创建一个空列表,用于存储不在输入集合中的结果数值。

  • 使用 for循环 遍历输入低索引和高索引。

  • 使用 if条件语句 检查当前元素是否不在频率集合的键值(使用 keys() 函数)中。

  • 使用append()函数将当前元素附加到结果列表中。

  • 打印不在给定输入范围内的集合中的结果列表。

示例

以下代码使用Counter()函数返回一个给定输入范围内不在输入集合中的数字列表 –

# importing Counter from collections
from collections import Counter
# input set
inputSet = {3, 10, 2, 11, 15, 4, 1}
# printing input set
print("Input set:", inputSet)
# getting the frequency of elements of the input set
setFrequency = Counter(inputSet)
# input low and high indexes
lowIndex, highIndex = 0, 8
# resultant list for storing numbers, not in the set
resultantList = []
# travsersing in a range from input low and high index
for i in range(lowIndex, highIndex):
    # checking whether the current element is not in keys of the set frequency
    if i not in setFrequency.keys():
        # appending current item to the resultant list
        resultantList.append(i)
print("Resultant list of elements not in a set within the specified range:\n", resultantList)

输出

执行上述程序后,将生成以下输出:

Input set: {1, 2, 3, 4, 10, 11, 15}
Resultant list of elements not in a set within the specified range:
 [0, 5, 6, 7]

结论

在这篇文章中,我们学会了使用3种不同的方法来查找范围内不在集合中的数字。此外,我们还学会了如何使用range()函数来获取指定索引之间的数字范围。最后,我们学会了如何使用Counter()函数来迅速高效地(O(1)时间复杂度)确定给定集合中是否存在元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程