如何从列表中删除 0.1%?
How to remove 0.1% from a list?
我有以下列表
data = [ '0.1%',
'hello',
'how are you guys',
'0.1%',
'1%',
'0.1%',
'1%', ]
我试过这个:
cell = float(data.rstrip("%"))
我想删除 1% 和 0.1%
预期输出:
data = [ 'hello','how are you guys' ]
给你:
new_list = [d for d in data if d not in ['0.1%', '1%']]
如果您想使用更通用的东西:
result = []
for i in data:
try:
float(i.strip('%'))
except:
result.append(i)
输出:
['hello', 'how are you guys']
这是另一种狡猾的方法!
data = ['0.1%',
'hello',
'how are you guys',
'0.1%',
'1%',
'0.1%',
'1%', ]
unwanted = [0, 3, 4, 5, 6,]
for ele in sorted(unwanted, reverse=True):
del data[ele]
print(data)
我有以下列表
data = [ '0.1%',
'hello',
'how are you guys',
'0.1%',
'1%',
'0.1%',
'1%', ]
我试过这个:
cell = float(data.rstrip("%"))
我想删除 1% 和 0.1%
预期输出:
data = [ 'hello','how are you guys' ]
给你:
new_list = [d for d in data if d not in ['0.1%', '1%']]
如果您想使用更通用的东西:
result = []
for i in data:
try:
float(i.strip('%'))
except:
result.append(i)
输出:
['hello', 'how are you guys']
这是另一种狡猾的方法!
data = ['0.1%',
'hello',
'how are you guys',
'0.1%',
'1%',
'0.1%',
'1%', ]
unwanted = [0, 3, 4, 5, 6,]
for ele in sorted(unwanted, reverse=True):
del data[ele]
print(data)