I am creating a list of dicts, then I want to sort the dicts in the list by the value of the key, lowest to highest.
Everything works except the sort:
def pythagorean(x1, y1, x2=0, y2=0):
return ((x1 - x2)**2 + (y1 - y2)**2)**0.5
points = [(-2, -4), (0, -2), (-1, 0), (3, -5), (-2, -3), (3, 2)]
dicts = []
for coord in points:
d = {}
a1, b1 = coord
distance = pythagorean(a1, b1)
d[distance] = (a1, b1)
dicts.append(d)
for i in dicts:
print(i)
dist_list = []
for item in dicts:
for key in item:
dist_list.append(key)
temp = sorted(dicts, key=lambda d: [k in d for k in dist_list])
print(temp)
I get the following output:
{4.47213595499958: (-2, -4)}
{2.0: (0, -2)}
{1.0: (-1, 0)}
{5.830951894845301: (3, -5)}
{3.605551275463989: (-2, -3)}
{3.605551275463989: (3, 2)}
[4.47213595499958, 2.0, 1.0, 5.830951894845301, 3.605551275463989, 3.605551275463989]
[{3.605551275463989: (-2, -3)},
{3.605551275463989: (3, 2)},
{5.830951894845301: (3, -5)},
{1.0: (-1, 0)},
{2.0: (0, -2)},
{4.47213595499958: (-2, -4)}]
That sort order is incorrect, at least as far as how I think it should be sorted: by the value of the key in the dict, from lowest to highest.
Using the keys in each dict as sort key works by converting them into a list:
>>> sorted(dicts, key=lambda d: list(d.keys()))
[{1.0: (-1, 0)},
{2.0: (0, -2)},
{3.605551275463989: (-2, -3)},
{3.605551275463989: (3, 2)},
{4.47213595499958: (-2, -4)},
{5.830951894845301: (3, -5)}]
This is one way:
lst = [{3.605551275463989: (-2, -3)}, {3.605551275463989: (3, 2)},
{5.830951894845301: (3, -5)}, {1.0: (-1, 0)},
{2.0: (0, -2)}, {4.47213595499958: (-2, -4)}]
list(map(dict, sorted(list(i.items()) for i in lst)))
# [{1.0: (-1, 0)},
# {2.0: (0, -2)},
# {3.605551275463989: (-2, -3)},
# {3.605551275463989: (3, 2)},
# {4.47213595499958: (-2, -4)},
# {5.830951894845301: (3, -5)}]
As @Mike stated, the trick is to cast as list()
the result of doing either dict.keys()
or dict.values()
. Otherwise you will get one of these errors:
TypeError: 'dict_keys' object is not subscriptable
TypeError: 'dict_values' object is not subscriptable
I'm adding a succinct copy/pasta demo here to help out the Google traffic.
import logging
# sort by keys
sorted(
[
{ level: getattr(logging, level) }
for level in
['WARNING', 'WARN', 'NOTSET', 'INFO', 'FATAL', 'ERROR', 'DEBUG', 'CRITICAL']
],
key=lambda o:list(o.keys())[0]
)
# returns:
# [
# {'CRITICAL': 50}, {'DEBUG': 10}, {'ERROR': 40}, {'FATAL': 50},
# {'INFO': 20}, {'NOTSET': 0}, {'WARN': 30}, {'WARNING': 30}
# ]
# sort by values
sorted(
[
{ level: getattr(logging, level) }
for level in
['WARNING', 'WARN', 'NOTSET', 'INFO', 'FATAL', 'ERROR', 'DEBUG', 'CRITICAL']
],
key=lambda o:list(o.values())[0]
)
# returns:
# [
# {'NOTSET': 0}, {'DEBUG': 10}, {'INFO': 20}, {'WARNING': 30},
# {'WARN': 30}, {'ERROR': 40}, {'FATAL': 50}, {'CRITICAL': 50}
# ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.