I want to learn more about Python and list (2 ).

Source: Internet
Author: User
Tags python list

I want to learn more about Python and list (2 ).

List operations

Merge list

List. append (x) is mentioned in the list operation in "a large list (1)", that is, an element x is appended to a known list.

In addition to appending an element to the list, the two lists can also be merged, or a list can be appended to another list. Follow the previous conventions to see the description in the official document:

List. extend (L)
Extend the list by appending all the items in the given list; equivalent to a [len (a):] = L.
Provide all the friends who are learning this content with a must to become a good programmer: Reading official documents is required.

This sentence in the official document is translated as follows:

Append all elements to a known list to expand it, which is equivalent to a [len (a)] = L
English is too bad and translation is too bad. Let's look at the example to better understand it.

>>> la[1, 2, 3]>>> lb['qiwsir', 'python']>>> la.extend(lb)>>> la[1, 2, 3, 'qiwsir', 'python']>>> lb['qiwsir', 'python']

The example above shows how to append the lb to the end of the two lists, one is la and the other is lb, that is, to add all the elements in the lb to la, expand la.

I must be curious when learning a program. In an interactive environment, I often experiment with my own ideas. Sometimes I am stupid.

>>> la = [1,2,3]>>> b = "abc">>> la.extend(b)>>> la[1, 2, 3, 'a', 'b', 'c']>>> c = 5>>> la.extend(c)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'int' object is not iterable

In the above experiment, What do you learn about the official website? Originally, if extend (str) is used, str is split in characters and then appended to la.

If the extend object is numeric, an error is returned.

Therefore, the extend object is a list. If it is str, python will first convert it to list by character and then add it to the known list.

However, do not forget the last half of the previous official document, which means:

>>> la[1, 2, 3, 'a', 'b', 'c']>>> lb['qiwsir', 'python']>>> la[len(la):]=lb>>> la[1, 2, 3, 'a', 'b', 'c', 'qiwsir', 'python']

List. extend (L) is equivalent to list [len (list):] = L. L is the list to be merged.

List Length

Do you still remember how to get the str length? What is its length? Can that method be used on the list? What is the effect?

Lab:

>>> name = 'qiwsir'>>> type(name)<type 'str'>>>> len(name)6>>> lname = ['sir','qi']>>> type(lname)<type 'list'>>>> len(lname)2>>> length = len(lname)>>> length2>>> type(length)<type 'int'>

Experiment conclusion:

Len (x), applicable to the same list
The number of elements in the list is displayed.
The returned value is of the int type.
Number of elements in the list

The above len (L) can obtain the length of the list, that is, the number of elements in the list. The python list also has an operation, that is, how many times a certain element appears in the list, that is, how many elements there are. This is what the official documentation says:

List. count (x)
Return the number of times x appears in the list.
Continuous experimentation is required to understand the refined expression in the document.

>>> La = [1, 1, 1, 3] >>> la. count (1) 3> la. append ('A') >>> la. append ('A') >>> la [1, 2, 1, 1, 3, 'A', 'a'] >>> la. count ('A') 2 >>> la. count (2) 1> la. count (5) # NOTE: There is no 5 in la, but if this method is used, no error is returned, and the number 00 is returned.

Position of an element in the list

As mentioned in "a large list (1)", the elements in the list can be numbered from left to right from 0 to create an index (if the index is from right to left, numbers starting from-1). An index can be used to extract an element or several elements. This is done as follows:

>>> la[1, 2, 3, 'a', 'b', 'c', 'qiwsir', 'python']>>> la[2]3>>> la[2:5][3, 'a', 'b']>>> la[:7][1, 2, 3, 'a', 'b', 'c', 'qiwsir']

If we consider the opposite, can we find the number in the list through an element?

The need to look at the official site is the direction of python. You can think of it as python.

>>> La [1, 2, 3, 'A', 'B', 'C', 'qiwsir ', 'python'] >>> la. index (3) 2> la. index ('A') 3 >>> la. index (1) 0 >>> la. index ('Qi') # if it does not exist, Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'qi 'is not in list >>> la. index ('qiwsir ') 6

List. index (x), x is an element in the list, so that you can retrieve the position of this element in the list. This is the real index. Pay attention to the index of the English word.

It is still the previous official explanation:

List. index (x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Is it clear?

Here, we will continue to talk about the list with high storage capacity.




Related Article

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.