python - Swapping maximum and minimum values in a list -
given list (for instance, [1,1,2,1,2,2,3]) not sorted highest lowest, , contains multiples of numbers, need swap, in place, maximums minimums, sec maxes sec mins, etc. so, our illustration list become [3,3,2,3,2,2,1].
also, clarify, it's not max , min, each layer of maxes , mins. if max 4, 1's , 4's should switch 2's , 3's.
i found question on topic: how swap maximums minimums? (python)
but code examples given seemed verbose, , assumed there no duplicates in list. there no improve way this? seems simple plenty thing.
this 1 way it, possible because python such expressive language:
>>> = [1,1,2,1,2,2,3] >>> d = dict(zip(sorted(set(a)), sorted(set(a), reverse=true))) >>> [d[x] x in a] [3, 3, 2, 3, 2, 2, 1]
python
No comments:
Post a Comment