["abc","123"] "1a2bc2" -> ['1','a','2','bc','2'] "1bdc3"-6ren">
gpt4 book ai didi

Python:如何分隔字符串中的字符和数字

转载 作者:行者123 更新时间:2023-12-04 14:58:17 24 4
gpt4 key购买 nike

我想知道如何分隔字符串中的字符和数字并将其作为数组返回

例如:

"abc123" -> ["abc","123"]

"1a2bc2" -> ['1','a','2','bc','2']

"1bdc3" -> ['1','bdc','3']

谢谢你抽空回答我

最佳答案

import itertools

def separate(string):
return ["".join(group) for key, group in itertools.groupby(string, str.isdigit)]

print(separate("abc123"))
print(separate("1a2bc2"))
print(separate("1bdc3"))

关于Python:如何分隔字符串中的字符和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67469276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com