在python上将字符串排序到列表中
我需要将一个字符串在python上将字符串排序到列表中,python,Python,我需要将一个字符串'abcdef'放入python上的列表['ab','cd','ef'] 我尝试使用list()但它返回['a','b','c','d','e','f'] 有人能帮忙吗???你可以使用zip: s = "abcdef" [''.join(x) for x in zip(s[::2], s[1::2])] # ['ab', 'cd', 'ef'] 或 您可以使用zip: s = "abcdef" [''.join(x) for x in zip(s[::2], s[1::2]
'abcdef'
放入python上的列表['ab','cd','ef']
我尝试使用list()
但它返回['a','b','c','d','e','f']
有人能帮忙吗???你可以使用
zip
:
s = "abcdef"
[''.join(x) for x in zip(s[::2], s[1::2])]
# ['ab', 'cd', 'ef']
或
您可以使用
zip
:
s = "abcdef"
[''.join(x) for x in zip(s[::2], s[1::2])]
# ['ab', 'cd', 'ef']
或
如果您不介意额外的功能,您可以使用如下内容:
def grouper(string, size):
i = 0
while i < len(string):
yield string[i:i+size]
i += size
如果您不介意额外的功能,您可以使用如下内容:
def grouper(string, size):
i = 0
while i < len(string):
yield string[i:i+size]
i += size