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