1-7 Python 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 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 = ['Google', 'Runoob', 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:

#!/usr/bin/python3
 
list1 = ['Google', 'Runoob', 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 or update data items in the list, you can also add a list item using append () method, as follows:

#!/usr/bin/python3
 
list = ['Google', 'Runoob', 1997, 2000]
 
print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])

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

Examples of the above output:

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

Delete list elements

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

#!/usr/bin/python3
 
list = ['Google', 'Runoob', 1997, 2000]
 
print ("原始列表 : ", list)
del list[2]
print ("删除第三个元素 : ", list)

Examples of the above output:

原始列表 :  ['Google', 'Runoob', 1997, 2000]
删除第三个元素 :  ['Google', 'Runoob', 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, end=" ") 1 2 3 Iteration

Python list interception with mosaic

Python list operation type string taken as follows:

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

operating:

Python expression result description
L[2] 'Taobao' Read the third element
L[-2] 'Runoob' Start reading the penultimate element from the right side: count from the right
L[1:] ['Runoob', 'Taobao'] All the elements of the output from the start of the second element
>>>L=['Google', 'Runoob', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'Runoob'
>>> L[1:]
['Runoob', 'Taobao']
>>>

The list also supports the splicing operation:

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

Nested list

Use nested lists to create another list that is in the list, such as:

>>>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)
listing the number of elements
2 max (list)
returns a list of the maximum element
3 min (list)
returns a list of the minimum element
4 list (seq)
converts a list of tuples

Python includes the following methods:

No. method
1 list.append (obj)
add new objects in the end of the list
2 list.count (obj)
count the number of an element that appears in the list
3 list.extend (seq)
disposable plurality of values is added at the end of the other sequence list (list with a new original extended list)
4 list.index (obj)
to find the location of an index value of the first match from the list
5 list.insert (index, obj)
the objects into the list
6 list.pop ([index = -1])
removes an element in the list (default to the last element), and returns the value of the element
7 list.remove (obj)
to remove the list a value of the first match
8 list.reverse()
反向列表中元素
9 list.sort( key=None, reverse=False)
对原列表进行排序
10 list.clear()
清空列表
11 list.copy()
复制列表

Guess you like

Origin blog.csdn.net/u012717715/article/details/91862775