Explain how to use the list and Ganso in Python _python

Source: Internet
Author: User
Tags stdin

List

One of the data types built into Python is the list: lists. A list is an ordered set of elements that you can add and delete at any time.

For example, if you list the names of all the students in your class, you can use a list to say:

>>> classmates = [' Michael ', ' Bob ', ' Tracy ']
>>> classmates
[' Michael ', ' Bob ', ' Tracy ']

The variable classmates is a list. Use the Len () function to get the number of list elements:

>>> Len (classmates)
3

Use an index to access the elements of each location in the list, and remember that the index starts at 0:

>>> classmates[0]
' Michael '
>>> classmates[1]
' Bob '
>>> classmates[ 2]
' Tracy '
>>> classmates[3]
traceback (most recent call last):
 File "<stdin>", Line 1, at <module>
Indexerror:list index out of range

Python reports a indexerror error when the index is out of range, so make sure the index is not crossed, and remember that the index of the last element is Len (classmates)-1.

If you want to take the last element, in addition to calculating the index position, you can also use-1 to index, directly to get the last element:

>>> classmates[-1]
' Tracy '

And so on, you can get the penultimate 2nd, the penultimate 3rd:

>>> classmates[-2] '
Bob '
>>> classmates[-3]
' Michael '
>>> classmates[-  4]
Traceback (most recent):
 File "<stdin>", line 1, <module>
indexerror:list Index Out of Range

Of course, the 4th one crosses the line.

The list is a mutable ordered table, so you can append elements to the end of the list:

>>> classmates.append (' Adam ')
>>> classmates
[' Michael ', ' Bob ', ' Tracy ', ' Adam ']

You can also insert an element into a specified location, such as a location with an index number of 1:

>>> Classmates.insert (1, ' Jack ')
>>> classmates
[' Michael ', ' Jack ', ' Bob ', ' Tracy ', ' Adam ']

To delete the element at the end of the list, use the Pop () method:

>>> classmates.pop ()
' Adam '
>>> classmates
[' Michael ', ' Jack ', ' Bob ', ' Tracy ']

To delete the element at the specified location, use the pop (i) method, where I is the index position:

>>> Classmates.pop (1)
' Jack '
>>> classmates
[' Michael ', ' Bob ', ' Tracy ']

To replace an element with another element, you can assign it directly to the corresponding index position:

>>> classmates[1] = ' Sarah '
>>> classmates
[' Michael ', ' Sarah ', ' Tracy ']

The data types of the elements in the list can also be different, such as:

>>> L = [' Apple ', 123, True]

The list element can also be another list, such as:

>>> s = [' Python ', ' Java ', [' asp ', ' php '], ' scheme ']
>>> len (s)
4

Note that s has only 4 elements, of which s[2] is also a list, which is easier to understand if it is opened:

>>> p = [' asp ', ' php ']
>>> s = [' Python ', ' Java ', p, ' scheme ']

To get ' php ' can write p[1] or s[2][1], so s can be seen as a two-dimensional array, similar to three-dimensional, four-dimension ... Array, but rarely used.

If an element in a list is not, it is an empty list with a length of 0:

>>> L = []
>>> len (l)
0

Tuple

Another ordered list is called a tuple: tuple. Tuple and list are very similar, but tuple can not be modified once initialized, for example, the name of the classmate is also listed:

>>> classmates = (' Michael ', ' Bob ', ' Tracy ')

Now, classmates this tuple can not be changed, it also has no append (), insert () such a method. The other way to get the element is the same as the list, you can use classmates[0],classmates[-1 normally, but you can't assign a value to another element.

What's the point of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, use tuple instead of list as much as possible with tuple.

Tuple trap: When you define a tuple, when defined, the elements of tuple must be identified, such as:

>>> T = (1, 2)
>>> T
(1, 2)

If you want to define an empty tuple, you can write ():

>>> t = ()
>>> T
()

However, to define a tuple that has only 1 elements, if you define this:

>>> t = (1)
>>> T
1

The definition is not tuple, it is 1 this number! This is because the parentheses () can represent both the tuple and the parentheses in the mathematical formula, which creates ambiguity, so Python stipulates that in this case, the calculation is done in parentheses, and the result is naturally 1.

Therefore, only 1 elements of the tuple definition must be added with a comma, to eliminate ambiguity:

>>> T = (1,)
>>> t
(1,)

Python also adds a comma when displaying a tuple of only 1 elements, lest you misunderstand the parentheses in the meaning of the mathematical calculation.

Finally, let's look at a "variable" tuple:

>>> t = (' A ', ' B ', [' A ', ' B ']]
>>> t[2][0] = ' X '
>>> t[2][1] = ' Y '
>>> T
(' A ', ' B ', [' X ', ' Y '])

This tuple definition has 3 elements, namely ' a ', ' B ' and a list. It's not that tuple once defined, does it not change? How come it's changed then?

Don't worry, let's take a look at the definition. Tuple contains 3 elements:

When we modify the list's elements ' A ' and ' B ' to ' X ' and ' Y ', the tuple becomes:

On the surface, the elements of tuple do change, but in fact they are not tuple elements, but the elements of the list. Tuple first point to the list did not change to another list, so, tuple so-called "invariant" is that tuple each element, point to never change. Point to ' a ', can't change to point ' B ', point to a list, can not be changed to point to other objects, but the list itself is variable!

Understanding the "point invariant", to create a content also unchanged tuple how to do? It must be ensured that every element of the tuple itself cannot be changed.
Summary

List and tuple are an ordered set of Python's built-in, one variable, one immutable. Choose to use them as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.