是否有Python内置数据类型,除此之外None
:
>>> not foo > None
True
Run Code Online (Sandbox Code Playgroud)
foo
这个类型的值在哪里?Python 3怎么样?
我需要按特定值对字典列表进行排序。不幸的是,有些值是 None 并且排序在 Python 3 中不起作用,因为它不支持 None 与非 None 值的比较。我还需要保留 None 值并将它们作为最低值放置在新的排序列表中。
编码:
import operator
list_of_dicts_with_nones = [
{"value": 1, "other_value": 4},
{"value": 2, "other_value": 3},
{"value": 3, "other_value": 2},
{"value": 4, "other_value": 1},
{"value": None, "other_value": 42},
{"value": None, "other_value": 9001}
]
# sort by first value but put the None values at the end
new_sorted_list = sorted(
(some_dict for some_dict in list_of_dicts_with_nones),
key=operator.itemgetter("value"), reverse=True
)
print(new_sorted_list)
Run Code Online (Sandbox Code Playgroud)
我在 Python 3.6.1 中得到了什么:
Traceback (most recent call last):
File "/home/bilan/PycharmProjects/py3_tests/py_3_sorting.py", line …
Run Code Online (Sandbox Code Playgroud)