Python 删除包含重复数字的数字

Python 删除包含重复数字的数字

在本文中,我们将学习如何在Python中删除包含重复数字的数字。

使用的方法

以下是完成此任务的各种方法:

  • 使用列表推导和set()函数

  • 使用re模块

  • 使用Counter()函数

示例

假设我们已经获得了一个包含数字的输入列表。现在我们将使用上述方法从列表中删除所有包含重复数字的数字,并返回结果列表。

输入

inputList = [3352, 8135, 661, 7893, 99]

输出

[8135, 7893]

在上面的输入列表中,第一个元素 3352 中,数字3重复了两次,因此被删除。但是 8135 没有重复的数字,因此保留。同样,661和99被删除,因为它们包含重复的字符。

因此,输出列表只包含8135和7893这两个元素。

方法1:使用列表推导和set()函数

len() - 使用len()方法返回对象中的项目数量。当对象是字符串时,len()函数返回字符串中的字符数。

set() 函数(创建一个集合对象。集合列表的顺序是随机的,因为其中的项目是无序的。它会删除所有重复的项目)。

步骤

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

  • 创建一个变量来存储 输入列表 ,并打印给定的列表。

  • 使用列表推导遍历给定列表的数字(元素)。

  • 使用str()函数将每个数字转换为字符串(将其转换为字符串的格式)。

  • 使用set()函数将该数字字符串转换为集合,该集合会删除该数字的重复数字。

  • 检查字符串(数字)的长度是否等于上述集合的长度。

  • 打印从输入列表中删除具有重复数字的元素后的结果列表。

示例

以下程序使用列表推导和set()函数从输入列表中删除包含重复数字的数字后返回结果列表:

# input list
inputList = [3352, 8135, 661, 7893, 99]

# printing the input list
print("Input list: ", inputList)

# Traversing through the numbers of the list using list comprehension

# Convering numbers to string and finding a set of strings (removes duplicates)

# Checking if the length of the set is equal to the number of digits
resultList = [i for i in inputList if len(set(str(i))) == len(str(i))]

# printing resultant list after removing elements with repeating digits
print("Resultant list after removing elements with repeating digits:")
print(resultList)

输出

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

Input list: [3352, 8135, 661, 7893, 99]
Resultant list after removing elements with repeating digits:
[8135, 7893]

时间复杂度 – O(n)

辅助空间 – O(n)

方法2:使用re模块

re.compile()方法

正则表达式模式可以组合为模式对象,然后可以使用re.compile()方法进行模式匹配。该方法还可以帮助在不重新编写模式的情况下再次搜索模式。

语法

re.compile(pattern, repl, string):

re.search()函数

在目标字符串中搜索正则表达式模式的所有匹配,并返回匹配位置的相应Match Object实例。它仅返回目标字符串中与模式的第一个匹配。

步骤

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

  • 使用import关键字导入re模块(正则表达式)。
  • 使用re模块的compile()函数,给出要删除具有重复数字的元素的正则表达式模式。
  • 遍历列表的元素,并使用search()函数检查列表元素是否与上述正则表达式模式匹配。

示例

以下程序使用re.complie()和re.search()函数从输入列表中删除包含重复数字的数字,并返回结果列表:

# importing re module
import re

# input list
inputList = [3352, 8135, 661, 7893, 99]

# printing the input list
print("Input list: ", inputList)

# regex pattern to remove elements with repeating digits
regexPattern = re.compile(r"(\d).*\1")

# Checking list elements for the above regex pattern matches
resultList = [i for i in inputList if not regexPattern.search(str(i))]

# printing resultant list after removing elements with repeating digits
print("Resultant list after removing elements with repeating digits:")
print(resultList)

输出

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

Input list: [3352, 8135, 661, 7893, 99]
Resultant list after removing elements with repeating digits:
[8135, 7893]

时间复杂度 − O(n)

辅助空间 − O(n)

方法3:使用Counter()函数

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

示例

以下程序使用Counter()函数从输入列表中移除包含重复数字的数字,并返回结果列表 –

# importing a Counter function from the collections module
from collections import Counter

# input list
inputList = [3352, 8135, 661, 7893, 99]

# printing the input list
print("Input list: ", inputList)

# Counter gives the unique keys(digits) of the number
resultList = [i for i in inputList if len(Counter(str(i))) == len(str(i))]

# printing resultant list after removing elements with repeating digits
print("Resultant list after removing elements with repeating digits:")
print(resultList)

输出

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

Input list: [3352, 8135, 661, 7893, 99]
Resultant list after removing elements with repeating digits:
[8135, 7893]

时间复杂度 – O(n)

辅助空间 – O(n)

该方法使用Counter()方法来计算数字的每个位数的频率。因此,它具有给定数字的唯一键(位数)。然后将给定数字的长度与计数器返回的唯一位数的数量进行比较。

结论

在本文中,我们学习了3种从一个列表中删除具有重复数字的整数的不同方法。此外,我们还学习了如何使用正则表达式模块在可迭代对象中查找模式匹配。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程