Python 排序DICT列表时,如何使用operator.itemgetter忽略None值?
我需要按特定值对字典列表进行排序。不幸的是,有些值是None,排序在Python3中不起作用,因为它不支持None与notnone值的比较。我还需要保留None值,并将它们作为最低值放在新的排序列表中 守则:Python 排序DICT列表时,如何使用operator.itemgetter忽略None值?,python,python-2.7,python-3.x,sorting,Python,Python 2.7,Python 3.x,Sorting,我需要按特定值对字典列表进行排序。不幸的是,有些值是None,排序在Python3中不起作用,因为它不支持None与notnone值的比较。我还需要保留None值,并将它们作为最低值放在新的排序列表中 守则: import operator list_of_dicts_with_nones = [ {"value": 1, "other_value": 4}, {"value": 2, "other_value": 3}, {"value": 3, "other_val
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)
我在Python 3.6.1中得到了什么:
Traceback (most recent call last):
File "/home/bilan/PycharmProjects/py3_tests/py_3_sorting.py", line 15, in <module>
key=operator.itemgetter("value"), reverse=True
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'
是的,我知道有与此类似的问题,但它们不涉及operator.itemgetter的特定用例:
我可以在不涉及字典的情况下,在Python3中重新创建Python2的排序行为。但我看不到一种使用操作符的方法。我找到了一种使用lambda key on value的方法。代码如下:
L = [ # I mixed them to shown the sorting
{"value": 1, "other_value": 4},
{"value": 2, "other_value": 3},
{"value": None, "other_value": 2},
{"value": 4, "other_value": 1},
{"value": None, "other_value": 42},
{"value": 3, "other_value": 9001}
]
def weighted(nb):
if nb is None:
return -float('inf')
else:
return nb
L.sort(key=lambda x:weighted(x["value"]), reverse=True)
print(L) # => return the expected output in python 3.6
也许还有另一种方法可以将“加权”函数写得更短,但它是有效的。其思想是返回-无限无值,然后按值排序
我希望这会有所帮助,我会先过滤掉
无
值,然后按照通常的方式进行排序:
my_list = [d for d in my_list if all(d.values())]
对于Python 3:如前所述,您可以在您的案例中执行以下操作:
L = [ # I mixed them to shown the sorting
{"value": 1, "other_value": 4},
{"value": 2, "other_value": 3},
{"value": None, "other_value": 2},
{"value": 4, "other_value": 1},
{"value": None, "other_value": 42},
{"value": 3, "other_value": 9001}
]
L.sort(key= lambda x: (x['value'] is not None, x['value']), reverse=True)
print(L)
>>>[{'value': 4, 'other_value': 1}, {'value': 3, 'other_value': 9001}, {'value': 2, 'other_value': 3}, {'value': 1, 'other_value': 4}, {'value': None, 'other_value': 2}, {'value': None, 'other_value': 42}]
maxsize
是Python数据结构的最大大小,例如列表中可以有多少个元素。
从技术上讲,这不是可能的最小整数(因为Python3具有无界整数值),但在正常使用情况下,它应该足够了
import operator
import sys
my_list = [
{"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}
]
def key(e):
v = e["value"]
return -sys.maxsize if v is None else v
new_sorted_list = sorted((my_list ),
key=key, reverse=True
)
print(new_sorted_list)
给
[{'value': 4, 'other_value': 1}, {'value': 3, 'other_value': 2}, {'value': 2, 'other_value': 3}, {'value': 1, 'other_value': 4}, {'value': None, 'other_value': 42}, {'value': None, 'other_value': 9001}]
[Program finished]
return-float('inf')如果nb不是其他nb
将是表示加权()的较短方式
[{'value': 4, 'other_value': 1}, {'value': 3, 'other_value': 2}, {'value': 2, 'other_value': 3}, {'value': 1, 'other_value': 4}, {'value': None, 'other_value': 42}, {'value': None, 'other_value': 9001}]
[Program finished]