Home  >  Article  >  Backend Development  >  Nested tuples in python list objects use the sort method to sort them

Nested tuples in python list objects use the sort method to sort them

不言
不言Original
2018-04-18 10:54:052964browse

The following article will share with you a sorting method based on sorting nested tuples in python list objects. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

Nesting tuples in a list, when sorting, produces a copy of the original array. During the sorting process, first sort from small to large according to the first field. If the first field is the same, it will be sorted according to the second field, and so on. When letters are involved, it will be sorted in lexicographic order.

is as follows:

a = [(1, 'B'), (1, 'A'), (1, 'C'), (1, 'AC'), (2, 'B'), (2, 'A'), (1, 'ABC')] 
a 
a.sort() 
a

The output result is:

[(1, 'B'), (1, 'A'), (1, 'C'), (1, 'AC'), (2, 'B'), (2, 'A'), (1, 'ABC')] 
[(1, 'A'), (1, 'ABC'), (1, 'AC'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B')]

Related recommendations:

Sort method when python list elements are tuples

The above is the detailed content of Nested tuples in python list objects use the sort method to sort them. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn