python - 如何从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 的输出始终为 []。
解决方案
也许您打算使用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
推荐阅读
- php - Observable 不返回 api 响应
- javascript - 隐藏/显示按钮的 CSS 样式
- java - 菜单绘图中的项目单击不起作用
- java - 在 tomcat 服务器上运行我的项目时出现以下错误。如何解决这个问题?
- arrays - F# - 基于百分位比较用标签替换浮点数
- c# - 从数据网格视图中删除一行
- android - 使用 SimpleDateFormat 问题将字符串转换为时间
- identityserver4 - identityserver4 - ArgumentNullException:值不能为空
- python - SQLAlchemy 列值基于另一个
- unity3d - Resonance Audio 中的房间效果之间的过渡?