Home > Software design >  Sort a defaultdict() by a value in Python3
Sort a defaultdict() by a value in Python3

Time:11-06

I have a defaultdict() having data in the following format

Time(Key)  [IP(Value), Count(Value)] 
defaultdict(<class 'list'>, {'[03:00]': [['104.194.24.33', 1], ['157.55.39.245', 3], ['17.58.102.43', 3], ['172.20.2.174', 1], ['173.249.54.67', 6], ['178.253.33.51', 6], ['2.177.12.140', 18], ['2.179.141.98', 6], ['2.185.221.79', 1], ['204.18.198.248', 10], ['207.46.13.104', 6], ['207.46.13.115', 7], ['207.46.13.136', 11], ['31.56.96.51', 22], ['34.247.132.53', 1], ['40.77.167.129', 10], ['46.224.77.32', 4], ['5.112.52.254', 2], ['5.160.157.20', 2], ['5.209.200.218', 21], ['5.211.97.39', 36], ['5.62.206.249', 1], ['5.78.180.75', 1], ['5.78.198.52', 16], ['51.15.15.54', 2], ['54.36.148.10', 1], ['54.36.148.117', 1], ['54.36.148.161', 1], ['54.36.148.17', 1], ['54.36.148.18', 1], ['54.36.148.232', 1], ['54.36.148.32', 1], ['54.36.148.87', 1], ['54.36.149.17', 1], ['54.36.149.35', 1], ['54.36.149.41', 1], ['54.36.149.58', 1], ['54.36.149.63', 1], ['54.36.149.70', 1], ['54.36.149.92', 1], ['66.111.54.249', 38], ['66.249.64.66', 1], ['66.249.66.194', 31], ['66.249.66.91', 20], ['89.199.193.251', 1], ['91.99.72.15', 16]]})

and I am trying to sort the the defaultdict() by the second element in the values (i.e. the Count value)

I looked at other posts and tried using

sorted(dct['[03:00]'],key=lambda kv:kv[1], reverse=True)

but it just keeps printing the same dictionary without any sorting. Any help would be kindly appreciated.

CodePudding user response:

Dictionaries before python 3.6(ish?) cannot be sorted because they have no order. If you have python < 3.6, you might want to check out OrderedDict, which can be sorted. For example:

>>> d = OrderedDict([('apple', 2), ('banana', 1)])
>>> d = sorted(d.iteritems(), key=lambda x: x[1])
>>> d
[('banana', 1), ('apple', 2)]

CodePudding user response:

sorted returns the result. You want .sort() if you want the dictionary value to be sorted in place:

from collections import defaultdict

dct = defaultdict(list)
dct['[03:00]'] = [['104.194.24.33', 1], ['157.55.39.245', 3], ['17.58.102.43', 3], ['172.20.2.174', 1], ['173.249.54.67', 6], ['178.253.33.51', 6], ['2.177.12.140', 18], ['2.179.141.98', 6], ['2.185.221.79', 1], ['204.18.198.248', 10], ['207.46.13.104', 6], ['207.46.13.115', 7], ['207.46.13.136', 11], ['31.56.96.51', 22], ['34.247.132.53', 1], ['40.77.167.129', 10], ['46.224.77.32', 4], ['5.112.52.254', 2], ['5.160.157.20', 2], ['5.209.200.218', 21], ['5.211.97.39', 36], ['5.62.206.249', 1], ['5.78.180.75', 1], ['5.78.198.52', 16], ['51.15.15.54', 2], ['54.36.148.10', 1], ['54.36.148.117', 1], ['54.36.148.161', 1], ['54.36.148.17', 1], ['54.36.148.18', 1], ['54.36.148.232', 1], ['54.36.148.32', 1], ['54.36.148.87', 1], ['54.36.149.17', 1], ['54.36.149.35', 1], ['54.36.149.41', 1], ['54.36.149.58', 1], ['54.36.149.63', 1], ['54.36.149.70', 1], ['54.36.149.92', 1], ['66.111.54.249', 38], ['66.249.64.66', 1], ['66.249.66.194', 31], ['66.249.66.91', 20], ['89.199.193.251', 1], ['91.99.72.15', 16]]
dct['[03:00]'].sort(key=lambda kv: kv[1], reverse=True)
print(dct)

Output:

defaultdict(<class 'list'>, {'[03:00]': [['66.111.54.249', 38], ['5.211.97.39', 36], ['66.249.66.194', 31], ['31.56.96.51', 22], ['5.209.200.218', 21], ['66.249.66.91', 20], ['2.177.12.140', 18], ['5.78.198.52', 16], ['91.99.72.15', 16], ['207.46.13.136', 11], ['204.18.198.248', 10], ['40.77.167.129', 10], ['207.46.13.115', 7], ['173.249.54.67', 6], ['178.253.33.51', 6], ['2.179.141.98', 6], ['207.46.13.104', 6], ['46.224.77.32', 4], ['157.55.39.245', 3], ['17.58.102.43', 3], ['5.112.52.254', 2], ['5.160.157.20', 2], ['51.15.15.54', 2], ['104.194.24.33', 1], ['172.20.2.174', 1], ['2.185.221.79', 1], ['34.247.132.53', 1], ['5.62.206.249', 1], ['5.78.180.75', 1], ['54.36.148.10', 1], ['54.36.148.117', 1], ['54.36.148.161', 1], ['54.36.148.17', 1], ['54.36.148.18', 1], ['54.36.148.232', 1], ['54.36.148.32', 1], ['54.36.148.87', 1], ['54.36.149.17', 1], ['54.36.149.35', 1], ['54.36.149.41', 1], ['54.36.149.58', 1], ['54.36.149.63', 1], ['54.36.149.70', 1], ['54.36.149.92', 1], ['66.249.64.66', 1], ['89.199.193.251', 1]]})
  • Related