打印给定数字中所有重复的数字,并按排序顺序显示
Python 拥有诸如 count、counter 和运算符函数等内置函数,可用于打印数字中所有重复的数字,并且按排序顺序显示。以下示例将帮助您更清楚地理解该概念。
示例
假设我们已经获取了一个**输入字符串**。我们现在将使用上述方法打印给定输入数字中所有重复/重复的数字,并按排序顺序显示。
输入
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() 函数来确定可迭代对象中有多少个元素。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP