python - Sort List of strings aternately -


so have list of strings :

['cbad','hfig','qspr','uyxz'] 

i want sort these strings in alternate fashion without changing relative position :

['abcd' ,'ihgf', 'pqrs', 'zyxu'] 

i.e. 'abcd' sorted in ascending order, 'ihgf' sorted in 'descending order, 'pqrs' sorted in ascending , on alternately.

so, instead of sorting these list of strings looping through list element wise, there other efficient way in python ? (keeping in mind list may contain large number of elements).

also, possible in such can define sort of custom order sorting, if want every third element sorted in descending order. :

['abcd' ,'fghi', 'srqp', 'uxyz'] 

this 1 liner works:

mylist = ['cbad','hfig','qspr','uyxz']  ["".join(sorted(s, reverse=i%2)) i,s in enumerate(mylist)]  

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -