|
发表于 2022-10-31 12:42:05
|
显示全部楼层
本帖最后由 tommyyu 于 2022-10-31 12:45 编辑
代码:- def out_unit(x, unit = ['kg']):
- if type(unit) == type(''):
- unit = [unit]
- unit.sort(key = len, reverse = True)
- for i in range(len(x)):
- for j in unit:
- if x[i].endswith(j):
- x[i] = x[i][:len(x[i])-len(j)]
- break
- return x
- print( out_unit(['26kg', '27kg', '28']) ) # 默认去除 kg 单位
- print( out_unit(['26km', '27km', '28'], unit = 'km') ) # 去除指定单位
- print( out_unit(['26km', '27kg', '28'], unit = ['km', 'kg']) ) # 去除多个指定单位
复制代码
运行效果:- ['26', '27', '28']
- ['26', '27', '28']
- ['26', '27', '28']
复制代码 |
|