gpt4 book ai didi

Python:拆分由给定对象类型分隔的字符串

转载 作者:行者123 更新时间:2023-11-28 20:22:36 25 4
gpt4 key购买 nike

如何分割分隔符是 Python 数据类型的字符串?

例如:

str_to_split = 'a1b2c'
str_to_split.split(int)
# ['a','b','c']

最佳答案

您可以使用 re.split() :

>>> import re
>>> thingToSplit = 'a1b2c'
>>> re.split('\d+', thingToSplit)
['a', 'b', 'c']

或者,申请 isdigit()检查:

>>> [item for item in thingToSplit if not item.isdigit()]
['a', 'b', 'c']

关于Python:拆分由给定对象类型分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22941637/

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