Python程式擴充套件字元頻率字串
在 Python 中,字串是最常使用的型別之一。只需用引號括起來即可輕鬆建立字串。Python 對單引號和雙引號的處理方式相同。為變數賦值和建立字串非常簡單。
在這篇文章中,我們將學習如何在 python 中擴充套件字元頻率字串。
使用的方法
以下是完成此任務的各種方法
使用 zip() 和 join() 函式
使用 re(正則表示式) 模組和 join() 函式
不使用任何內建函式
示例
假設我們已經獲取了一個包含字元及其頻率的輸入字串。我們現在將根據以下頻率擴充套件這些字元。
輸入
inputString = 'p5y3t6h2o1n4'
輸出
Resultant string after expanding − pppppyyytttttthhonnnn
在此輸入字串中,字元“p”擴充套件了 5 次,“y”擴充套件了 3 次,……依此類推,根據字元後面的頻率進行擴充套件。
使用 zip() 和 join() 函式
在這種方法中,我們將使用 python 的 zip() 和 join() 函式來擴充套件字元頻率字串並列印輸出。
語法
join()
join() 是 Python 中一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。
zip()
zip() 函式可用於組合兩個列表/迭代器。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存包含字元及其頻率的輸入字串。
列印輸入字串。
遍歷字串的 zip() 函式以組合迭代器,其中第一個迭代器遍歷字元,第二個迭代器遍歷頻率。
將第一個迭代器乘以第二個迭代器以重複/擴充套件字元。
使用 join() 函式將此 zip 物件轉換為字串。
擴充套件後列印結果字串。
示例
以下程式使用 zip() 和 join() 函式返回輸入字串的擴充套件字元頻率字串。
# input string containing characters followed by their frequency.
inputString = 'p5y3t6h2o1n4'
# printing input string
print("Input String: ", inputString)
# Creating a pair(p,q) using zip function
# where p stands for character and q stands for its frequency
# Multiplying character(p) with q to repeat q times
expandStr = "".join(p * int(q)
for p, q in zip(inputString[0::2], inputString[1::2]))
# printing the resultant string after expanding
print("Resultant string after expanding:", expandStr)
輸出
執行上述程式後,將生成以下輸出。
Input String: p5y3t6h2o1n4 Resultant string after expanding: pppppyyytttttthhonnnn
時間複雜度:O(n)
輔助空間:O(n)
使用 re(正則表示式) 模組和 join() 函式
在這種方法中,我們將使用正則表示式模組和 join 函式來擴充套件字元頻率字串。
re.findall()−
findall() 函式返回字串中模式的所有不重疊匹配項,作為一個字串列表。字串從左到右掃描,匹配項按發現順序返回。
以下程式使用正則表示式模組和 join() 函式返回輸入字串的擴充套件字元頻率字串。
# importing re i.e, regex module
import re
# input string containing characters followed by their frequency.
inputString = 'p5y3t6h2o1n4'
# printing input string
print("Input String: ", inputString)
# Longer digit strings can be included by using findall
# to match together numbers and characters independently.
expandStr = ''.join(charactr * int(n or 1)
for charactr, n in re.findall(r'(\w)(\d+)?', inputString))
# printing the resultant string after expanding
print("Resultant string after expanding:", expandStr)
輸出
執行上述程式後,將生成以下輸出。
Input String: p5y3t6h2o1n4 Resultant string after expanding: pppppyyytttttthhonnnn
不使用任何內建函式
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存包含字元及其頻率的輸入字串。
列印輸入字串。
初始化一個空列表,用於儲存字元。
初始化另一個空列表,用於儲存其相應的頻率。
使用for 迴圈遍歷輸入字串的每個字元,直到其長度(使用len() 函式,返回物件中的專案數)。
使用if 條件語句檢查當前索引是否為偶數(使用模運算子%)。
如果條件為true,則使用append() 函式(在末尾將元素新增到列表中)將當前索引處的相應元素追加到字元列表中
否則,將該頻率字元作為整數追加到頻率列表中。
建立一個空字串,用於儲存結果擴充套件字串。
再次使用for 迴圈遍歷字元列表,直到其長度。
將當前索引處的字元乘以其頻率,並使用“+”運算子將其連線到結果擴充套件字串。
擴充套件後列印結果字串。
示例
以下程式返回輸入字串的擴充套件字元頻率字串,不使用任何內建函式。
# input string containing characters followed by their frequency.
inputString = 'p5y3t6h2o1n4'
# printing input string
print("Input String: ", inputString)
# empty list for storing characters
charsList = []
# empty list for storing their frequencies
freqList = []
# traversing through each character of the input string till its length
for p in range(0, len(inputString)):
# checking whether the current index is even
if(p % 2 == 0):
# appending that corresponding element at the current index to
# the characters list if the condition is true
charsList.append(inputString[p])
else:
# otherwise appending that @@ to the frequency list
freqList.append(int(inputString[p]))
# storing resultant expanded string
expandStr = ""
# traversing through the characters list till its length
for p in range(0, len(charsList)):
# multiplying character at current index with its frequency
# and concatenating it to the expanded string
expandStr += charsList[p]*freqList[p]
# printing the resultant string after expanding
print("Resultant string after expanding:", expandStr)
輸出
執行上述程式後,將生成以下輸出。
Input String: p5y3t6h2o1n4 Resultant string after expanding: pppppyyytttttthhonnnn
結論
在本文中,我們學習了三種不同的擴充套件字元頻率字串的方法。此外,我們還學習瞭如何在使用 zip() 函式組合迭代器時遍歷它們。最後,透過使用模運算子(%),演示了一種無需使用任何內建函式即可解決此問題的簡單方法。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP