python - PYTHON 使用空值、字符串和数值对列表进行排序
问题描述
我想对短列表进行排序,例如:
# we can have only 3 types of value: any string numeric value like '555', 'not found' and '' (can have any variation with these options)
row = ['not found', '', '555']
到
# numeric values first, 'not found' less prioritize and '' in the end
['555', 'not found', '']
我尝试使用
row.sort(key=lambda x: str(x).isnumeric() and not bool(x))
但它不工作
我该如何排序?(数值在前,“未找到”优先级较低,最后是“”)
解决方案
def custom_sort(list):
L1 = []
L2 = []
L3 = []
for element in list:
if element.isnumeric():
L1.append(element)
if element == 'Not found':
L2.append(element)
else : L3.append(element)
L1.sort()
L1.append(L2).append(L3)
return L1
推荐阅读
- matlab - 将数字转换为用户生成的输入(MATLAB)
- vb.net - 如何使用代码更改 vbnet 中的按钮文本
- javascript - 浏览器中的OpenGL或其他图像处理,没有画布?
- c++ - 类似于 Knight-Tour-Problem 的回溯问题
- python - 为什么我在 if 语句中提出问题时会出现错误
- arduino - 在 if 语句中使用 & 运算符
- node.js - 从非 www 重定向到 www 域名后 cookie 丢失
- excel - 使用 vba-excel 从在线共享点文件夹下载 Zip 文件
- c# - 如何在 EF Core 5 Fluent API 中配置具有继承关系的值对象
- python - 有没有更好的方法来为深度嵌套的字典定义棉花糖模式?