Python程序,用于获取K个切片的字符串的所有可能切片
给定一个字符串,任务是编写一个Python程序来获取 K 个切片的所有可能切片。
Input : test_str = “Gfg4all”, K = 3
Output : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]
Explanation : All possible 3 slices for constructing string are returned.
Input : test_str = “Gfg4all”, K = 2
Output : [[‘G’, ‘fg4all’], [‘Gf’, ‘g4all’], [‘Gfg’, ‘4all’], [‘Gfg4’, ‘all’], [‘Gfg4a’, ‘ll’], [‘Gfg4al’, ‘l’]]
Explanation : All possible 2 slices for constructing string are returned.
方法#1:使用列表理解+切片+循环
在这种情况下,连续切片从大小 1 开始递增计算。最后一个元素总是以 acc 中的所有方式拆分为 2。到其他字符串。
Python3
# Python3 code to demonstrate working of
# All possible slices for K length
# Using list comprehension + string slicing + loop
# initializing string
test_str = "Gfg4all"
# printing original string
print("The original string is : " + str(test_str))
# initializing number of slices
K = 3
res = [[test_str]]
for idx in range(K - 1):
# slicing initial strings with difference sizes.
res = [[*strt, end[:y], end[y:]] for *strt, end in res
for y in range(1, len(end) - K + idx + 2)]
# printing result
print("All possible slices for K strings : " + str(res))
Python3
# Python3 code to demonstrate working of
# All possible slices for K length
# Using combinations() + zip() + list comprehension
from itertools import combinations
# initializing string
test_str = "Gfg4all"
# printing original string
print("The original string is : " + str(test_str))
# initializing number of slices
K = 3
# combinations used to perform all possible slices
res = [[test_str[idx: j] for idx, j in zip([None, *sub], [*sub, None])]
for sub in combinations(range(1, len(test_str)), K - 1)]
# printing result
print("All possible slices for K strings : " + str(res))
输出:
The original string is : Gfg4all
All possible slices for K strings : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]
方法#2:使用组合() + zip() +列表理解
在这种情况下,组合用于获取迭代期间使用切片计算的范围的所有可能子串。
蟒蛇3
# Python3 code to demonstrate working of
# All possible slices for K length
# Using combinations() + zip() + list comprehension
from itertools import combinations
# initializing string
test_str = "Gfg4all"
# printing original string
print("The original string is : " + str(test_str))
# initializing number of slices
K = 3
# combinations used to perform all possible slices
res = [[test_str[idx: j] for idx, j in zip([None, *sub], [*sub, None])]
for sub in combinations(range(1, len(test_str)), K - 1)]
# printing result
print("All possible slices for K strings : " + str(res))
输出:
The original string is : Gfg4all
All possible slices for K strings : [[‘G’, ‘f’, ‘g4all’], [‘G’, ‘fg’, ‘4all’], [‘G’, ‘fg4’, ‘all’], [‘G’, ‘fg4a’, ‘ll’], [‘G’, ‘fg4al’, ‘l’], [‘Gf’, ‘g’, ‘4all’], [‘Gf’, ‘g4’, ‘all’], [‘Gf’, ‘g4a’, ‘ll’], [‘Gf’, ‘g4al’, ‘l’], [‘Gfg’, ‘4’, ‘all’], [‘Gfg’, ‘4a’, ‘ll’], [‘Gfg’, ‘4al’, ‘l’], [‘Gfg4’, ‘a’, ‘ll’], [‘Gfg4’, ‘al’, ‘l’], [‘Gfg4a’, ‘l’, ‘l’]]