Python程序拆分加入连续的相似字符
给定一个字符串,我们的任务是编写一个Python程序,在出现非相似字符进行拆分。
Input : test_str = ‘ggggffggisssbbbeessssstt’
Output : [‘gggg’, ‘ff’, ‘gg’, ‘i’, ‘sss’, ‘bbb’, ‘ee’, ‘sssss’, ‘tt’]
Explanation : All similar consecutive characters are converted to separate strings.
Input : test_str = ‘ggggffgg’
Output : [‘gggg’, ‘ff’, ‘gg’]
Explanation : All similar consecutive characters are converted to separate strings.
方法 #1:使用join() +列表推导+ groupby()
其中,使用 groupby() 对字符进行相似性分组,使用 join() 来重组字符串列表。列表理解执行迭代构造组的任务。
Python3
# Python3 code to demonstrate working of
# Split joined consecutive similar characters
# Using join() + list comprehension + groupby()
from itertools import groupby
# initializing string
test_str = 'ggggffggisssbbbeessssstt'
# printing original string
print("The original string is : " + str(test_str))
# groupby groups the elements, join joining Consecutive groups
res = ["".join(group) for ele, group in groupby(test_str)]
# printing result
print("Consecutive split string is : " + str(res))
Python3
# Python3 code to demonstrate working of
# Split joined consecutive similar characters
# Using finditer() + regex + list comprehension
import re
# initializing string
test_str = 'ggggffggisssbbbeessssstt'
# printing original string
print("The original string is : " + str(test_str))
# list comprehension iterates for all the formed groups found by regex
# if consecutive numbers need to search "d" can be used.
res = [iters.group(0) for iters in re.finditer(r"(\D)\1*", test_str)]
# printing result
print("Consecutive split string is : " + str(res))
输出:
The original string is : ggggffggisssbbbeessssstt
Consecutive split string is : [‘gggg’, ‘ff’, ‘gg’, ‘i’, ‘sss’, ‘bbb’, ‘ee’, ‘sssss’, ‘tt’]
方法#2:使用 finditer() + regex + list comprehension
在这种情况下,正则表达式用于检查连续的相等序列。 finditer() 执行在字符串中查找匹配正则表达式的任务。
蟒蛇3
# Python3 code to demonstrate working of
# Split joined consecutive similar characters
# Using finditer() + regex + list comprehension
import re
# initializing string
test_str = 'ggggffggisssbbbeessssstt'
# printing original string
print("The original string is : " + str(test_str))
# list comprehension iterates for all the formed groups found by regex
# if consecutive numbers need to search "d" can be used.
res = [iters.group(0) for iters in re.finditer(r"(\D)\1*", test_str)]
# printing result
print("Consecutive split string is : " + str(res))
输出:
The original string is : ggggffggisssbbbeessssstt
Consecutive split string is : [‘gggg’, ‘ff’, ‘gg’, ‘i’, ‘sss’, ‘bbb’, ‘ee’, ‘sssss’, ‘tt’]