📜  Python|在第 K 次出现字符时拆分字符串

📅  最后修改于: 2022-05-13 01:55:43.875000             🧑  作者: Mango

Python|在第 K 次出现字符时拆分字符串

过去已经处理过很多拆分问题,但有时也会遇到这样的问题,我们需要在第 K 个字符出现时拆分字符串。让我们讨论一些可以解决这个问题的方法。

方法 #1:使用 split() + join()
split 和 join 方法可以执行此特定任务。在此,我们根据字符拆分字符串,并根据 K,使用嵌套连接方法执行重新连接。

Python3
# Python3 code to demonstrate working of
# Split string on Kth Occurrence of Character
# Using split() + join()
 
# initializing string
test_str = "Geeks_for_Geeks_is_best"
 
# split char
splt_char = "_"
 
# initializing K
K = 3
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using split() + join()
# Split string on Kth Occurrence of Character
temp = test_str.split(splt_char)
res = splt_char.join(temp[:K]), splt_char.join(temp[K:])
 
# printing result
print("Is list after Kth split is : " + str(list(res)))


Python3
# Python3 code to demonstrate working of
# Split string on Kth Occurrence of Character
# Using regex
import re
 
# initializing string
test_str = "Geeks_for_Geeks_is_best"
 
# split char
splt_char = "_"
 
# initializing K
K = 3
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using regex
# Split string on Kth Occurrence of Character
temp = [x.start() for x in re.finditer(splt_char, test_str)]
res1 = test_str[0:temp[K - 1]]
res2 = test_str[temp[K - 1] + 1:]
res = (res1 + " " + res2).split(" ")
 
# printing result
print("Is list after Kth split is : " + str(res))


Python3
test_str = "Geeks for geeks"
 
K = 1 #kth occurrence
 
new_str, rest = test_str.split(" ", K)
                                
#or if you want a list
                                
new_str_list = test_str.split(" ", K)
 
print(new_str)
print(rest)
 
print(f'list: {new_str_list}')


输出
The original string is : Geeks_for_Geeks_is_best
Is list after Kth split is : ['Geeks_for_Geeks', 'is_best']


方法#2:使用正则表达式
也可以使用正则表达式来解决此问题以执行此特定任务。与切片一起,正则表达式库的 finditer 方法可以帮助实现这一特定任务。

Python3

# Python3 code to demonstrate working of
# Split string on Kth Occurrence of Character
# Using regex
import re
 
# initializing string
test_str = "Geeks_for_Geeks_is_best"
 
# split char
splt_char = "_"
 
# initializing K
K = 3
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using regex
# Split string on Kth Occurrence of Character
temp = [x.start() for x in re.finditer(splt_char, test_str)]
res1 = test_str[0:temp[K - 1]]
res2 = test_str[temp[K - 1] + 1:]
res = (res1 + " " + res2).split(" ")
 
# printing result
print("Is list after Kth split is : " + str(res))
输出
The original string is : Geeks_for_Geeks_is_best
Is list after Kth split is : ['Geeks_for_Geeks', 'is_best']

方法 #3:使用 split 中的 maxsplit

您可以使用 split 本身来获取方法 split 的第二个参数的第 k 个数字,即最大拆分,然后您就有了字符串。

可能是最简单的方法

Python3

test_str = "Geeks for geeks"
 
K = 1 #kth occurrence
 
new_str, rest = test_str.split(" ", K)
                                
#or if you want a list
                                
new_str_list = test_str.split(" ", K)
 
print(new_str)
print(rest)
 
print(f'list: {new_str_list}')
输出
Geeks
for geeks
list: ['Geeks', 'for geeks']