3 examples of 'python sorted multiple keys' in Python

Every line of 'python sorted multiple keys' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
88def sorted_keys(keys, startkey, reverse=False):
89 """Generator that yields sorted keys starting with startkey
90
91 Parameters
92 ----------
93
94 keys: Iterable of tuple/list
95 \tKey sequence that is sorted
96 startkey: Tuple/list
97 \tFirst key to be yielded
98 reverse: Bool
99 \tSort direction reversed if True
100
101 """
102
103 tuple_key = lambda t: t[::-1]
104 if reverse:
105 tuple_cmp = lambda t: t[::-1] > startkey[::-1]
106 else:
107 tuple_cmp = lambda t: t[::-1] < startkey[::-1]
108
109 searchkeys = sorted(keys, key=tuple_key, reverse=reverse)
110 searchpos = sum(1 for _ in ifilter(tuple_cmp, searchkeys))
111
112 searchkeys = searchkeys[searchpos:] + searchkeys[:searchpos]
113
114 for key in searchkeys:
115 yield key
458def sorted(self, key=None, reverse=False):
459 """Similar to the built-in :func:`sorted`, except this method returns
460 a new :class:`OrderedMultiDict` sorted by the provided key
461 function, optionally reversed.
462
463 Args:
464 key (callable): A callable to determine the sort key of
465 each element. The callable should expect an **item**
466 (key-value pair tuple).
467 reverse (bool): Set to ``True`` to reverse the ordering.
468
469 >>> omd = OrderedMultiDict(zip(range(3), range(3)))
470 >>> omd.sorted(reverse=True)
471 OrderedMultiDict([(2, 2), (1, 1), (0, 0)])
472
473 Note that the key function receives an **item** (key-value
474 tuple), so the recommended signature looks like:
475
476 >>> omd = OrderedMultiDict(zip('hello', 'world'))
477 >>> omd.sorted(key=lambda i: i[1]) # i[0] is the key, i[1] is the val
478 OrderedMultiDict([('o', 'd'), ('l', 'l'), ('e', 'o'), ('h', 'w')])
479 """
480 cls = self.__class__
481 return cls(sorted(self.iteritems(), key=key, reverse=reverse))
144def testKeys(self):
145 """ Test output of OrderedDict.keys(). """
146 self.assertEqual(self.odict.keys(),
147 ['first', 'third', 'fourth', 'fifth'])

Related snippets