Python Crawler Notes _ list operations

Source: Internet
Author: User

Lists are the most basic data structures in Python, and the list is the most commonly used Python data type, and the list's data items do not need to have the same type. Each element in the list is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has 6 built-in types of sequences, but the most common are lists and tuples. Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

One, create a list
Just enclose the different data items separated by commas in square brackets. As shown below:

List1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5];list3 = ["A", "B", "C", "D"];

As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.
Second, access the value in the list
Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7];p rint "list1[0]:", list1[0]p Rint "List2[1:5]:", List2[1:5] output of the above instance: List1[0]: Physicslist2[1:5]: [2, 3, 4, 5]

Third, update the list
You can modify or update the list's data items, or you can use the Append () method to add the list items as follows:

#!/usr/bin/pythonlist = [' Physics ', ' Chemistry ', 1997,];p rint "Value available at index 2:" Print list[2];list[2] = 2001;print "New value available at index 2:" Print list[2];

The output of the above example is:

Value available at index 2:1997new value available at index 2:2001

Use the Append () method to add list items

>>> s=[' physics ', ' Chemistry ']>>> s.append ("Wangtao") >>> s[' physics ', ' chemistry ', ' Wangtao ']

Iv. Deleting list elements
You can use the DEL statement to delete the elements of a list, as in the following example:

#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997,];p rint list1;del list1[2];p rint "after deleting value at Inde X 2: "Print list1;

The output of the above example is:

[' Physics ', ' Chemistry ', 1997, 2000] After deleting value at index 2: [' Physics ', ' chemistry ', 2000]


V. Python list script operators
The operands of the list to + and * are similar to strings. The + sign is used for the combined list, and the * number is used for repeating lists.

As shown below:

Python Expressions Results Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [1, 2, 3] True Whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iteration

Vi. Python list interception
The list of Python intercepts the type of string manipulation as follows:

L = [' spam ', ' spam ', ' spam! ']

Operation:

Python Expressions Results Description
L[2] ' spam! ' Read the third element in the list
L[-2] ' Spam ' Reads the second-to-last element in a list
L[1:] [' Spam ', ' spam! '] To intercept a list starting with the second element


Vii. functions and methods of Python list operations
The list operation contains the following functions:
1. CMP (List1, List2): Compare elements of two lists
2. Len (list): Number of elements
3. Max (list): Returns the maximum value of the element
4, Min (list): Returns the minimum value of the list element
5. List (seq): Convert tuples to lists
The list operation contains the following methods:
1. List.append (obj): Add a new object at the end of the list
2. List.count (obj): Count the number of occurrences of an element in a list
3. List.extend (seq): Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4, List.index (obj): Find the index position of the first occurrence of a value from the list
5. List.insert (index, obj): inserting objects into the list
6, List.pop (Obj=list[-1]): Removes an element from the list (the last element by default), and returns the value of the element
7, List.remove (obj): Removes the first occurrence of a value in a list
8, List.reverse (): Reverse List of elements
9. List.sort ([func]): Sort the original list

The Extend () function is used to append multiple values from another sequence at the end of the list (the original list is expanded with a new list)

#!/usr/bin/pythonalist = [123, ' xyz ', ' Zara ', ' abc ', 123];blist = [, ' Manni '];alist.extend (blist) print ' Extended List : ", alist;

The output of the above experiment:

$ python extend.pyextended List: [123, ' xyz ', ' Zara ', ' abc ', 123, ' Manni ']

Python Crawler Notes _ list operations

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.