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()方法給出了數字中每個數字的頻率。因此,它具有給定數字的唯一鍵(數字)。然後將給定數字的長度與計數器返回的唯一數字數量進行比較。
結論
在這篇文章中,我們學習了三種不同的方法來從列表中移除包含重複數字的整數。此外,我們還學習瞭如何使用正則表示式模組在可迭代物件中查詢模式匹配。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP