Python 按排序顺序打印给定数字中的所有重复数字
Python具有内部函数count、counter和operator函数,可用于打印所有重复出现的数字,还可以按排序顺序进行。以下示例将帮助您更清楚地理解这个概念。
示例
假设我们使用一个 输入字符串 。现在,我们将使用上述方法按排序顺序打印给定输入数字中的所有重复/重复数字。
输入
inputNum = 5322789124199
输出
1 2 9
在上述输入数字中, 2、1和9 是重复的数字。因此,这些数字按升序排序。
因此输出按排序顺序排列的重复数字为 1、2、9 。
使用count()函数
字符串count()函数
返回字符串中给定值出现的次数。
语法
string.count(value, start, end)
sort()函数
sort()函数可以对原始列表进行排序。它表示sort()方法会改变列表元素的顺序。
list.sort()
join()函数
join()是Python中的一个字符串函数,用于连接由字符串分隔符分隔的序列的元素。该函数连接序列元素以转换为字符串。
步骤
下面是执行所需任务的算法/步骤:
- 创建一个变量来存储 输入的数字 。
-
为存储输入数字中重复数字结果的 空列表 进行初始化。
-
使用 str() 函数将输入数字转换为字符串。
-
使用for循环遍历字符串中的每个字符。
-
使用 if条件语句 检查当前字符的频率是否大于1,使用 count() 函数,并且它不在结果重复列表中。
-
如果条件为 true ,则使用 append()函数 (将元素添加到列表末尾)将该字符附加到结果列表(重复列表)中。
-
使用 sort() 函数对结果重复列表按升序进行排序。
-
使用 join() 函数将结果重复列表转换为字符串并打印出来。
示例
以下程序使用count()函数按排序顺序返回输入数字中的所有重复数字-
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# traversing through each character if a string
for c in newString:
# checking whether the frequency of the current character is greater than 1
# and it is not present in a resultant duplicates list
if(newString.count(c) > 1 and c not in duplicatesList):
# appending that character to the duplicates list if the condition is true
duplicatesList.append(c)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))
输出
在执行时,上述程序将生成以下输出结果 –
1 2 9
使用哈希算法
下面的程序使用哈希算法以排序顺序返回输入数字中所有重复的数字:
示例
def getDuplicateNum(inputNum):
# setting the count of all the digits (from 0 to 9) to 0
count = [0] * 10
# converting the input number to a string
newString = str(inputNum)
# traversing through each character of a string
for c in newString:
# getting the integer value of the current character
curr_digit = int(c)
# incrementing the count of digits by 1
count[curr_digit] += 1
# Traversing in frequency(count)
for k in range(10):
# checking whether the frequency of the digit is greater than 1
if (count[k] > 1):
# printing that digit of the count is greater than 1
print(k, end=" ")
# input number
inputNum = 5322789124199
# calling the above defined getDuplicateNum() by passing
# input number as an argument to it
getDuplicateNum(inputNum)
输出
1 2 9
使用Counter()函数
以下程序使用Counter()函数以排序顺序返回输入数字中出现的所有重复数字:
示例
# importing Counter from the collections module
from collections import Counter
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# getting the frequency of each character of a string as a key-value pair
charFrequency = Counter(newString)
# traversing through the key, value pairs of characters frequency
for k, v in charFrequency.items():
# checking whether current value is greater than 1(repeating)
if v > 1:
# appending that corresponding key to the duplicates list if the condition is true
duplicatesList.append(k)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))
输出
1 2 9
使用operator.countOf()函数
operator.countOf函数
operator模块的countOf()函数返回在a中等于b的元素数量。
语法
operator.countOf(a, b)
参数
- a – 列表或字符串或任何其他数据类型。
-
b – 需要在“a”中计算出现次数的值。
以下程序使用operator.countOf()函数以排序的方式返回输入数字中的所有重复数字 –
示例
import operator as op
# input number
inputNum = 5322789124199
# storing resultant repeating digits in a number in a list
duplicatesList = []
# converting input number to a string
newString = str(inputNum)
# traversing through each character if a string
for c in newString:
# checking whether the frequency of the current character is greater than 1
# and it is not present in a resultant duplicates list
if(op.countOf(newString, c) > 1 and op.countOf(duplicatesList, c) == 0):
# appending that character to duplicates list if the condition is true
duplicatesList.append(c)
# sorting the resultant duplicates list
duplicatesList.sort()
# converting resultant duplicates list to string
print(' '.join(duplicatesList))
输出
1 2 9
结论
在本文中,我们学习了如何按照排序顺序打印给定数字中的所有重复数字。使用新的函数operator.countOf()函数来确定可迭代对象中有多少个元素也是我们学习的另一件事情。