[PYTHON] List type, tuple type

Master the list type and tuple type

Sort the list The list type in Python makes it easy to sort by changing the order of the elements. You can sort the elements in ascending order by calling the sort () method on a list that has numbers as elements. Since the list type is a mutable data type, the list object itself is rewritten as a result of calling the sort () method.

[Sort in ascending order] monk_fish_team = [158, 157, 163, 157, 145] monk_fish_team.sort() monk_fish_team ↓ 145, 157, 157, 158, 163

The default behavior of calling the sort () method with no arguments is to sort the numbers in ascending order. This is because programs often do such processing. However, you can change the sorting method by giving an argument. For example, if you have a keyword argument of reverse and pass True, you can sort in descending order.

[Sort in descending order] monk_fish_team = [158, 157, 163, 157, 145] monk_fish_team.sort(reverse = True) monk_fish_team ↓ 163, 158, 157, 157, 145

Customize the sort order The operation of sorting is nothing but the process of comparing the size and superiority of data and deciding the order. The sort () method is based on something other than a simple number, by giving a separate criterion for determining the order. You can control the sort order.

[Sort () method] S.sort(Key, reverse)

The argument of the sort () method is different from other methods, and it is mandatory to specify the keyword of the argument. You can customize the sort order by passing a function that returns the criteria that determine the order to Key. Also, as I introduced earlier, the reverse argument is specified as True when the sort order is descending. The default is Falle, which sorts in ascending order.

We will use a concrete example to show you how to customize the sort order. The tank used by a girls' school in Ibaraki prefecture that was used for the string type split () method. Consider sorting tanks in order of strength based on data such as name, speed, and armor thickness. First, consider how to represent the data for one tank. The tuple type is suitable for managing heterogeneous data such as names and numbers side by side. For each tank, tuple the data of name, speed, armor thickness, and caliber of the main gun. Write as "(" Type 89 Medium Tank ", 20, 17, 57)". Arrange this data for 5 cars.

[Make a list of tuples] tank_data = [(“Panzer IV”, 38, 80, 75), (“LT-38”, 42, 50, 37), ("Type 89 Medium Tank", 20, 17, 57), ("Sturmgeschutz III", 40, 50, 75), ("M3 Medium Tank", 39, 51, 75)]

In order to sort this data, we need to clearly define what we have to be strong. It is difficult to define it exactly, but here it is simply "the larger the value of speed, armor thickness, and main gun caliber is, the stronger it is." I will make a rule. Create a function that returns the elements of the list as numbers. In this case, if you pass a tuple of tank data in the list All you have to do is create a function that adds the specifications and returns. Create a function that adds and returns the first and subsequent numbers in the tuple. [Function evaluate_tankdata () that adds and returns tank specifications] def evaluate_tankdata(tap): return tup[1]+tup[2]+tup[3]

You can use the evaluate_tankdata () function above to compare tanks numerically. Let's quantify the strength of the tank using the list of tuples we have defined. [Display the strength of each tank] evaluate_tankdata(tank_data[0]) ↓ 193

evaluate_tankdata(tank_data[4]) ↓ 165

You can now compare the data in the list. Pass this function as the argument key of the sort () method. Then, the elements of the list are passed to the function and evaluated, and the elements are sorted. Now, let's actually sort the data using the sort () method. After that, try to display the sorted list. [Sort by tank strength] tank_data,sort(key=evaluate_tankdata, reverse=True) tank_data ↓ [(“Panzer IV”, 38, 80, 75), (“Sturmgeschutz III”, 40, 50, 75), (“M3 Medium Tank”, 39, 51, 75), (“LT-38” , 42, 50, 37), ("Type 89 Medium Tank", 20, 17, 57)]

The default behavior of the sort () method is to sort in ascending order. True is passed to the Reverse argument to sort the order in descending order, in order of strength, that is, in descending order of the numerical value returned by the evaluate_tankdata () function. It turned out that the performance of Panzer IV was the highest. In this way, you can customize the behavior of the sort () method in detail by creating a function that evaluates the elements of the list and giving arguments.

Recommended Posts

List type, tuple type 2
List type, tuple type
Shell type
List comprehension
linked list
[Django] Convert QuerySet to dictionary type list
[Introduction to Udemy Python3 + Application] 21. Tuple type
Dictionary type 2
Dictionary type 1
Join list
python> Convert tuple to list> aList = list (pi_tuple)
List comprehension
Basic grammar of Python3 series (list, tuple)
[Python Iroha] Difference between List and Tuple
[Python] list
[Introduction to Udemy Python3 + Application] 16. List type
Basic operation list of Python3 list, tuple, dictionary, set
TypeError: unsupported operand type (s) for /:'list' and'float'
Differences and commonalities between dict, list, and tuple types