4 examples of 'python sorted key lambda' in Python

Every line of 'python sorted key lambda' 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
this disclaimer
314def realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT):
315 """
316 Convenience function to properly sort signed floats.
317
318 A signed float in a string could be "a-5.7". This is a wrapper around
319 ``natsorted(seq, alg=ns.REAL)``.
320
321 The behavior of :func:`realsorted` for `natsort` version >= 4.0.0
322 was the default behavior of :func:`natsorted` for `natsort`
323 version < 4.0.0.
324
325 Parameters
326 ----------
327 seq : iterable
328 The input to sort.
329
330 key : callable, optional
331 A key used to determine how to sort each element of the sequence.
332 It is **not** applied recursively.
333 It should accept a single argument and return a single value.
334
335 reverse : {{True, False}}, optional
336 Return the list in reversed sorted order. The default is
337 `False`.
338
339 alg : ns enum, optional
340 This option is used to control which algorithm `natsort`
341 uses when sorting. For details into these options, please see
342 the :class:`ns` class documentation. The default is `ns.REAL`.
343
344 Returns
345 -------
346 out : list
347 The sorted input.
348
349 See Also
350 --------
351 index_realsorted : Returns the sorted indexes from `realsorted`.
352
353 Examples
354 --------
355 Use `realsorted` just like the builtin `sorted`::
356
357 >>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
358 >>> natsorted(a)
359 ['num2', 'num5.3', 'num5.10', 'num-3']
360 >>> realsorted(a)
361 ['num-3', 'num2', 'num5.10', 'num5.3']
362
363 """
364 return natsorted(seq, key, reverse, alg | ns.REAL)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

17def sorted_key_from_cmp(cmp_func, key_func=None):
18 class _key_proxy:
19
20 __slots__ = ('_obj',)
21
22 if key_func: # done this way for speed reasons.
23 def __init__(self, obj, key_convert=key_func):
24 self._obj = key_convert(obj)
25 else:
26 def __init__(self, obj):
27 self._obj = obj
28
29 def __lt__(self, other, _cmp_func=cmp_func):
30 return _cmp_func(self._obj, other._obj) < 0
31
32 return _key_proxy
70def sorted(lst, cmp=None, key=None, reverse=None):
71 "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
72 sorted_lst = list(lst)
73 sorted_lst.sort(cmp, key, reverse)
74 return sorted_lst
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

Related snippets