I want to sort the list, but I want it to be sorted, excluding the first element.
For instance:
a = ['T', 4, 2, 1, 3]
Now I want the list to be sorted, but the first element should remain in its place:
a = ['T', 1, 2, 3, 4]
I know this can be done using a sorting algorithm, but is there a way to do this one or more Python ways to do this?
You can do this by the purpose of the slice, where you replace the fragment awith a sorted fragment a:
a
>>> a = ['T',4,2,1,3] >>> a[1:] = sorted(a[1:]) >>> a ['T', 1, 2, 3, 4]
You can cut it, sort the final slice and concatenate it later:
>>> a = a[:1] + sorted(a[1:]) >>> a ['T', 1, 2, 3, 4]
Source: https://habr.com/ru/post/1676317/More articles:Getting source string using lxml tostring () - pythonMaximum value for tag limit exceeded InfluxDB - influxdbVS 2015 vs 2017 javascript intellisense (d3.js) - javascriptТипы семей не могут возвращать тип RankN - обходные пути или альтернативы? - typesCreate a data block with grouped questions from three columns - pythonHow to transfer dynamodb data to change the main table? - amazon-dynamodbgeolocation.getCurrentPosition does not work at night - How to provide my own api key? - javascriptWhy am I getting an index out of range error when base64 encodes an array of bytes? - arraysRace condition even when using sync.Mutex in golang - mutexPython: best way to split a list of objects by field? - pythonAll Articles