Python - 在每 K 个元素之后的每个重复字符串中插入字符
给定一个字符串和一个字符,在每 K 次出现后插入一个字符。
Input : test_str = ‘GeeksforGeeks’, K = 2, add_chr = “;”
Output : [‘;GeeksforGeeks’, ‘Ge;eksforGeeks’, ‘Geek;sforGeeks’, ‘Geeksf;orGeeks’, ‘Geeksfor;Geeks’, ‘GeeksforGe;eks’, ‘GeeksforGeek;s’]
Explanation : All combinations after adding ; after every K elements.
Input : test_str = ‘GeeksforGeeks’, K = 2, add_chr = “*”
Output : [‘*GeeksforGeeks’, ‘Ge*eksforGeeks’, ‘Geek*sforGeeks’, ‘Geeksf*orGeeks’, ‘Geeksfor*Geeks’, ‘GeeksforGe*eks’, ‘GeeksforGeek*s’]
Explanation : All combinations after adding * after every K elements.
方法#1:使用循环+字符串切片
这是可以执行此任务的方法之一。在这种情况下,我们使用字符串切片在第 K 个出现处对字符串进行切片,并在它们之间附加字符。
Python3
# Python3 code to demonstrate working of
# Insert character after every K elements
# Using loop + string slicing
# Function to Insert character
# in each duplicate string
# after every K elements
def insertCharacterAfterKelements(test_str, K, char):
res = []
# using loop to iterate
for idx in range(0, len(test_str), K):
# appending all the results
res.append(test_str[:idx] + char + test_str[idx:])
return str(res)
# Driver Code
# initializing string
input_str = 'GeeksforGeeks'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 2
# initializing add char
add_chr = ";"
# printing result
print("The extracted strings : " +
insertCharacterAfterKelements(input_str, K, add_chr))
Python3
# Python3 code to demonstrate working of
# Insert character after every K elements
# Using list comprehension + string slicing
# Function to Insert character
# in each duplicate string
# after every K elements
def insertCharacterAfterKelements(test_str, K, char):
# list comprehension to bind logic in one.
res = [test_str[:idx] + char + test_str[idx:]
for idx in range(0, len(test_str), K)]
return str(res)
# Driver Code
# initializing string
input_str = 'GeeksforGeeks'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 2
# initializing add char
add_chr = ";"
# printing result
print("The extracted strings : " +
insertCharacterAfterKelements(input_str, K, add_chr))
输出:
The original string is : GeeksforGeeks
The extracted strings : [‘;GeeksforGeeks’, ‘Ge;eksforGeeks’, ‘Geek;sforGeeks’, ‘Geeksf;orGeeks’, ‘Geeksfor;Geeks’, ‘GeeksforGe;eks’, ‘GeeksforGeek;s’]
方法#2:使用列表理解+字符串切片
这是可以执行此任务的另一种方式。在这方面,我们执行与循环差异类似的任务,即使用列表理解作为解决这个问题的速记。
蟒蛇3
# Python3 code to demonstrate working of
# Insert character after every K elements
# Using list comprehension + string slicing
# Function to Insert character
# in each duplicate string
# after every K elements
def insertCharacterAfterKelements(test_str, K, char):
# list comprehension to bind logic in one.
res = [test_str[:idx] + char + test_str[idx:]
for idx in range(0, len(test_str), K)]
return str(res)
# Driver Code
# initializing string
input_str = 'GeeksforGeeks'
# printing original string
print("The original string is : " + str(input_str))
# initializing K
K = 2
# initializing add char
add_chr = ";"
# printing result
print("The extracted strings : " +
insertCharacterAfterKelements(input_str, K, add_chr))
输出:
The original string is : GeeksforGeeks
The extracted strings : [‘;GeeksforGeeks’, ‘Ge;eksforGeeks’, ‘Geek;sforGeeks’, ‘Geeksf;orGeeks’, ‘Geeksfor;Geeks’, ‘GeeksforGe;eks’, ‘GeeksforGeek;s’]