列印給定數字中所有重複的數字,並按排序順序顯示
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