Книга: Practical Programming, Fourth Edition
Назад: Modifying Lists
Дальше: Slicing Lists

Operations on Lists

, and , introduced a few of Python’s built-in functions. Some of these, such as len, can be applied to lists, as well as others you haven’t seen before. (See the following table.)


Table 5. List Operations

Function

Description

len

Returns the number of items in list L

max

Returns the maximum value in list L

min

Returns the minimum value in list L

sum

Returns the sum of the values in list L

sorted

Returns a copy of list L where the items are in order from smallest to largest. This function does not modify L.


Here are some examples. The half-life of a radioactive substance is the time taken for half of it to decay. After this time has passed twice, three-quarters of the material will have decayed; after three times, seven-eighths will have decayed, and so on.

An isotope is a form of a chemical element. Plutonium has several isotopes, each with a different half-life. Here are some of the built-in functions in action, working on a list of the half-lives of plutonium isotopes Pu-238, Pu-239, Pu-240, Pu-241, and Pu-242.

 >>>​​ ​​half_lives​​ ​​=​​ ​​[887.7,​​ ​​24100.0,​​ ​​6563.0,​​ ​​14,​​ ​​373300.0]
 >>>​​ ​​len(half_lives)
 5
 >>>​​ ​​max(half_lives)
 373300.0
 >>>​​ ​​min(half_lives)
 14
 >>>​​ ​​sum(half_lives)
 404864.7
 >>>​​ ​​sorted(half_lives)
 [14, 887.7, 6563.0, 24100.0, 373300.0]
 >>>​​ ​​half_lives
 [887.7, 24100.0, 6563.0, 14, 373300.0]

In addition to built-in functions, some of the operators that you have seen can also be applied to lists. Like strings, lists can be combined using the concatenation (+) operator:

 >>>​​ ​​original​​ ​​=​​ ​​[​​'H'​​,​​ ​​'He'​​,​​ ​​'Li'​​]
 >>>​​ ​​final​​ ​​=​​ ​​original​​ ​​+​​ ​​[​​'Be'​​]
 >>>​​ ​​final
 ['H', 'He', 'Li', 'Be']

This code doesn’t mutate either of the original list objects. Instead, it creates a new list whose entries refer to the items in the original lists.

Concatenation

A list has a type, and Python complains if you misuse a value of some type. For example, an error occurs when the concatenation operator is applied to a list and a string:

 >>>​​ ​​[​​'H'​​,​​ ​​'He'​​,​​ ​​'Li'​​]​​ ​​+​​ ​​'Be'
 Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
  ['H', 'He', 'Li'] + 'Be'
  ~~~~~~~~~~~~~~~~~~^~~~~~
 TypeError: can only concatenate list (not "str") to list

You can also multiply a list by an integer to get a new list containing the elements from the original list repeated that number of times:

 >>>​​ ​​metals​​ ​​=​​ ​​[​​'Fe'​​,​​ ​​'Ni'​​]
 >>>​​ ​​metals​​ ​​*​​ ​​3
 ['Fe', 'Ni', 'Fe', 'Ni', 'Fe', 'Ni']

As with concatenation, the original list isn’t modified; instead, a new list is created.

One operator that does modify a list is del (“delete”). It can be used to remove an item from a list by position, as follows:

 >>>​​ ​​metals​​ ​​=​​ ​​[​​'Fe'​​,​​ ​​'Ni'​​]
 >>>​​ ​​del​​ ​​metals[0]
 >>>​​ ​​metals
 ['Ni']

The in Operator on Lists

The in operator can be applied to lists to check whether an object is a member of a list:

 >>>​​ ​​nobles​​ ​​=​​ ​​[​​'helium'​​,​​ ​​'neon'​​,​​ ​​'argon'​​,​​ ​​'krypton'​​,​​ ​​'xenon'​​,​​ ​​'radon'​​]
 >>>​​ ​​gas​​ ​​=​​ ​​input(​​'Enter a gas: '​​)
 Enter a gas: argon
 >>>​​ ​​if​​ ​​gas​​ ​​in​​ ​​nobles:
 ...​​ ​​print(f​​'{gas.capitalize()} is noble.'​​)
 ...
 Argon is noble.
 >>>​​ ​​gas​​ ​​=​​ ​​input(​​'Enter a gas: '​​)
 Enter a gas: nitrogen
 >>>​​ ​​if​​ ​​gas​​ ​​in​​ ​​nobles:
 ...​​ ​​print(f​​'{gas.capitalize()} is noble.'​​)
 ...
 >>>

Unlike with strings, when used with lists, the in operator checks only for a single item. This code checks whether the list [1, 2] is an item in the list [0, 1, 2, 3]:

 >>>​​ ​​[1,​​ ​​2]​​ ​​in​​ ​​[0,​​ ​​1,​​ ​​2,​​ ​​3]
 False
Назад: Modifying Lists
Дальше: Slicing Lists