Latest web development tutorials

Python3 list

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 types 6 sequence, but the most common are lists and tuples.

Sequence of operations can be carried out, including indexing, slicing, add, multiply, check the members.

Moreover, Python has a built-determine the length of the sequence and determining the maximum and minimum element method.

Python is a list of the most commonly used type of data, it can be used as a comma-separated values ​​appear in square brackets.

List of data items need not have the same type

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

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

With the index of the string as a list of index starts from 0. List can be intercepted, combinations and the like.


Access list value

Use subscripting to access values ​​in the list, you can also use square brackets in the form of interception of character, as follows:

#!/usr/bin/python3

list1 = ['Google', 'w3big', 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]:  Google
list2[1:5]:  [2, 3, 4, 5]

update list

You can modify the list of data items or updates, you can also use append () method to add a list of items, as follows:
#!/usr/bin/python3

list = ['Google', 'w3big', 1997, 2000]

print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])

We will discuss the use of the next chapter in the append () method: Note

Examples of the above output:

第三个元素为 :  1997
更新后的第三个元素为 :  2001

Remove list element

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

#!/usr/bin/python3

list = ['Google', 'w3big', 1997, 2000]

print (list)
del list[2]
print ("删除第三个元素 : ", list)

Examples of the above output:

删除第三个元素 :  ['Google', 'w3big', 2000]

We will discuss the use of the next chapter in the remove () method: Note


Python script list operator

List of + and * operators and string similarity. + Sign for the combined list, an asterisk for the repeat list.

As follows:

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

Python list interception and splicing

Python list interception string type of operation, as follows:

L=['Google', 'w3big', 'Taobao']

operating:

Python expression result description
L [2] 'Taobao' Read the third element
L [-2] 'W3big' The second element of the countdown start reading from the right: count from the right
L [1:] [ 'W3big', 'Taobao'] Output all elements from the start of the second element
>>> L=['Google', 'w3big', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'w3big'
>>> L[1:]
['w3big', 'Taobao']
>>> 

The list also supports the splicing operation:

>>> squares = [1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Nested list

Nested list is created using the other lists in the list, for example:

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

Python list of functions & methods

Python includes the following functions:

No. function
1 len (list)
The number of list elements
2 max (list)
Back to the list of elements Max
3 min (list)
Returns a list of the minimum elements
4 list (seq)
Will be converted to a list of tuples

Python includes the following methods:

No. method
1 list.append (obj)
In the end of the list to add new objects
2 list.count (obj)
Number of times an element statistics appear in the list
3 list.extend (seq)
Multiple values ​​at the end of the list of additional disposable another sequence (extension of the original list with a new list)
4 list.index (obj)
Find the index position of the first occurrence of a value from a list
5 list.insert (index, obj)
Insert objects into a list
6 list.pop (obj = list [-1] )
Remove one element in the list (by default the last element), and returns the value of the element
7 list.remove (obj)
Remove the list a value of the first match
8 list.reverse ()
Reverse list element
9 list.sort ([func])
The original list is sorted
10 list.clear ()
Clear List
11 list.copy ()
Copy List