python list (list) function and use

  

Python sequence is the most basic data structure. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has a built-in type 6 sequences, but the most common are lists and tuples.

The sequence of operations can be carried out, including indexing, slicing, add, multiply, check members.

Further, Python already built determined sequence length and determining the maximum and minimum element method.

Python is a list of the most common types of data, it can appear as a comma-separated values ​​in square brackets.

Data items list need not have the same type

Create a list, as long as the comma-separated data items using different brackets can be. As follows:

list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]

Like string indices, list indices start at 0. The list can be intercepted, combinations and the like.


The value of the access list

The subscript index to access the values ​​in the list, you can also use the same form of brackets taken character, as follows:

Example (Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]

Examples of the above output:

list1[0]:  physics
list2[1:5]: [2, 3, 4, 5]

update list

You can modify or update data items in the list, you can also add a list item using append () method, as follows:

Example (Python 2.0+)

! # / usr / bin / Python # - * - Coding: UTF-. 8 - * - List = [ ] ## empty list List . the append ( ' the Google ' ) ## using append () additive element List . the append ( ' Runoob ' ) Print List

Note: Use discussions we will append () method in the next section

Examples of the above output:

['Google', 'Runoob']

Delete list elements

You can use the del statement to remove elements of the list, the following examples:

Example (Python 2.0+)

#!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1

Examples of the above output:

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

Note: Use discussions we will remove () method in the next section


Python list script operator

Similar lists of + and * operators string. + Number combination for the list, the list for repeatedly asterisk.

As follows:

Python expression result description
only ([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 elements present in the list
for x in [1, 2, 3]: print x, 1 2 3 Iteration

Python interception list

Python list taken following examples:

>>>L = ['Google', 'Runoob', 'Taobao'] >>> L[2] 'Taobao' >>> L[-2] 'Runoob' >>> L[1:] ['Runoob', 'Taobao'] >>>

description:

Python expression result description
L[2] 'Taobao' Read the third element in the list
L[-2] 'Runoob' Read the list of the penultimate element
L[1:] ['Runoob', 'Taobao'] From the beginning of the second element interception list

 

Guess you like

Origin www.cnblogs.com/68xi/p/11622340.html