gpt4 book ai didi

python - 在python中从字符串中的其他字符中拆分数字

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:44 25 4
gpt4 key购买 nike

我有这样的字符串; '17.', '0,5', ',5', 'CO2-heidet', '1990ndatel'等,我想拆分如下:['17', '.'], ['0', ',', '5'], [' ,', '5'], ['CO', '2', '-heidet'], ['1990', 'ndatel']

我怎样才能在 python 中有效地做到这一点?

最佳答案

您也可以使用 itertools.groupby()使用作为str.isdigit实现这一点:

>>> from itertools import groupby
>>> my_list = ['17.', '0,5', ',5', 'CO2-heidet', '1990ndatel']

>>> [[''.join(j) for i, j in groupby(l, str.isdigit)] for l in my_list]
[['17', '.'], ['0', ',', '5'], [',', '5'], ['CO', '2', '-heidet'], ['1990', 'ndatel']]

关于python - 在python中从字符串中的其他字符中拆分数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051859/

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