Python|以 n 个连续字符为一组分割字符串
给定一个字符串(无论是数字字符串还是字符字符串),编写一个Python程序将字符串按每个第 n个字符分割。
例子:
Input : str = "Geeksforgeeks", n = 3
Output : ['Gee', 'ksf', 'oor', 'gee', 'ks']
Input : str = "1234567891234567", n = 4
Output : [1234, 5678, 9123, 4567]
方法 #1:使用列表推导
# Python code to split string
# by every 3rd number
# String initialization
string = "Geeksforgeeks"
# Defining splitting point
n = 3
# Using list comprehension
out = [(string[i:i+n]) for i in range(0, len(string), n)]
# Printing output
print(out)
输出:
['Gee', 'ksf', 'org', 'eek', 's']
方法 #2:使用zip_longest
# Python code to split string of number
# and character into every 4th number
# Importing
from itertools import zip_longest
# Group function using zip_longest to split
def group(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
# String initialization
str = '123GeeksForGeeks4567'
# Split point
n=4
# list of separated string
out_string = [''.join(lis) for lis in group(n, str, '')]
# Output list initialization
out_no = []
# Converting list of string into list of integer
for a in out_string:
out_no.append(a)
# Printing list
print(out_no)
输出:
['123G', 'eeks', 'ForG', 'eeks', '4567']