Python 扩展字符频率字符串
在Python中, 字符串 是最常用的类型之一。你只需将字符用引号括起来即可创建字符串。Python将单引号和双引号视为相同。赋值给变量并创建字符串非常直观。
在本文中,我们将学习如何在Python中扩展字符频率字符串。
使用的方法
以下是完成此任务的各种方法:
- 使用zip()和join()函数
-
使用re(regex)模块和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(regex)模块和join()函数
在这种方法中,我们将使用regex模块和join函数来扩展字符频率字符串。
re.findall()−
findall() 函数返回字符串中模式的所有非重叠匹配项作为字符串列表。字符串从左到右进行扫描,匹配项按照它们被找到的顺序返回。
以下程序使用 regex 模块和 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()函数循环遍历迭代器并将它们组合起来。最后,我们还展示了一种不使用任何内置函数的直接解决此问题的方法,该方法利用了模运算(%)操作符。