我有一个字符串'3,M,8,M'
,想把它转换成一个列表[3, 'M', 8, 'M']
。有谁知道我该怎么做?
非常感谢您提前!
您可以将拆分与列表理解结合起来:
'3,M,8,M'.split(',')
# Out[4]: ['3', 'M', '8', 'M']
[int(x) if x.isdigit() else x for x in '3,M,8,M'.split(',')]
# Out[5]: [3, 'M', 8, 'M']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句