我有这样的字符串; '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/