首页 > 解决方案 > 如何从python中的行中删除字母和空格?

问题描述

我是 python 新手,我有这样的问题:

我有一个清单

Index = ['TH', '1', '1', '2', '28', '29', '2', '']

我想得到

max(Index)

到目前为止我有这样的东西

import numbers
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
Index1 = [x for x in Index if x] #to remove empty space
Index2 = [x for x in Index1 if isinstance(x, numbers.Number)] #to remove all letters
Index3 = map(int, Index2)
print (max(Index3))

但 Index1 和 Index2 的输出始终为 []。

标签: pythonline

解决方案


也许您打算使用isdigit()字符串的方法:

Index = ['TH', '1', '1', '2', '28', '29', '2', '']
Index1 = [x for x in Index if x] #to remove empty space
Index2 = [x for x in Index1 if x.isdigit()] #to remove all letters
Index3 = map(int, Index2)
print (max(Index3))

输出:

29

推荐阅读