1 Listand theTuple1.1 List(ordered repeatable modifiable)
A list is an ordered set of elements that can be added and removed at any time.
>>> list=[1,2,3]
>>> List
[1, 2, 3]
>>> list=[' 1 ', ' 2 ', ' 3 ']
>>> List
[' 1 ', ' 2 ', ' 3 ']
>>> len (list)-- displays The number of elements in the list
3
>>>List[0] --the number of element indexes starts from 0 , that is, from left to right the first is 0 , and Redis Similar in
' 1 '
>>> List[1]
' 2 '
>>> List[2]
' 3 '
>>>List[-1] --The penultimate, the rightmost element , similar to Redis
' 3 '
>>> List[-2]
' 2 '
The list is a mutable, ordered table, so you can append an element to the end of the list :
>>> list.append (' 4 ')-- Note the brackets, not []
>>> List
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
>>> List.append (4)
>>> List
[' 1 ', ' 2 ', ' 3 ', ' 4 ', 4]
inserts an element into the specified position , such as where the index number is 1
>>> List.insert (1, ' x ')--1 represents the index position,x represents the element
>>> List
[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]
Delete Element , the last bit is deleted by default, and you can specify an element at a location
>>> List
[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]
>>> List.pop ()-- Delete the last element by default
4-- returns the deleted element
>>> List
[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ']
>>> List
[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ']
>>> List.pop (1)-- Specify the deletion of the 1-position element
' X '
>>> List
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
To replace an element with another element , you can assign a value directly to the corresponding index position
>>> List
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
>>> list[0]= ' 0 '
>>> List
[' 0 ', ' 2 ', ' 3 ', ' 4 ']
List The data type of the elements inside can also be different
>>> List
[' 1 ', ' X ', ' 2 ', ' 3 ', ' 4 ', 4]
List in list
>>> List
[' 1 ', ' 2 ', ' 3 ', ' 4 ']
>>> List.insert (1,[' 1 ', ' 2 '])
>>> List
[' 1 ', [' 1 ', ' 2 '], ' 2 ', ' 3 ', ' 4 ']
>>> List[1][1]-- Some like two-dimensional arrays
' 2 '
Empty List
>>> l=[]
>>> Len (L)
0
1.2 Tuple(orderly, repeatable, non-modifiable)
It is also an ordered list,and the tuple and list are very similar, but the tuple cannot be modified once initialized.
>>> (t=)
>>> T
(1, 2)
>>> t= ()-- empty tuple
>>> Len (t)
0
when there is only one element in a tuple, the definition takes a comma
>>> t= (1,)
>>> T
(1,)
>>> T[0]
1
Create a "variable" tuple
>>> t= (1,2,[1,2])
>>> t[2][0]= ' x '
>>> t[2][1]= ' y '
>>> T
(1, 2, [' X ', ' Y '])
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1795431
Python List &tuple