Skip to main content
 首页 » 编程设计

python之如何从字符串中删除特定数字

2025年05月04日37zdz8207

代码如下:

aList = ['0.01', 'xyz', 'J0.01', 'abc', 'xyz']; 
aList.remove('0.01'); 
print("List : ", aList) 

这是输出: list :

['xyz', 'J0.01', 'abc', 'xyz'] 

如何删除附加到“J0.01”的 0.01?我想保留 J。感谢您的宝贵时间! =)

请您参考如下方法:

看来你想要

aList = ['0.01', 'xyz', 'J0.01', 'abc', 'xyz']; 
>>> [z.replace('0.01', '') for z in aList] 
 
['', 'xyz', 'J', 'abc', 'xyz'] 

如果您还想删除空字符串/空格,

>>> [z.replace('0.01', '') for z in aList if z.replace('0.01', '').strip()] 
['xyz', 'J', 'abc', 'xyz']