Chapter II List and tuples
2.1 Overview of the sequence
Sequence: Each element is assigned an ordinal
6 kinds: list, tuple, string, Unicode string, buffer object, Xrange object
Sequences can be nested sequences
Python container concepts: sequences, dictionaries, collections
2.2 Operation of a general-purpose sequence
index, Shard, add, multiply, belong, length, maximum, minimum, iteration
2.2.1 Index
>>> greeting= ' Hello '
>>> Greeting[0]
H
>>> Greeting[-1]
' O '
>>> ' Hello ' [1]
E
2.2.2 Shards
>>> numbers=[1,2,3,4,5,6,7,8]
>>> Numbers[3:6] #第一个元素包含, the second element does not contain a
[4, 5, 6]
>>> Numbers[0:1]
[1]
1. Elegant shortcuts
>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> Numbers[7:10]
[8, 9, 10]
>>> Numbers[-3:-1] #左边的元素一定要比右边的元素早出现
[8, 9]
>>> numbers[-3:]
[8, 9, 10]
>>> Numbers[:3]
[1, 2, 3]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2. Longer stride length
>>> Numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> Numbers[::4]
[1, 5, 9]
Step is negative
>>> Numbers[8:3:-1]
[9, 8, 7, 6, 5]
>>> Numbers[10:0:-2]
[10, 8, 6, 4, 2]
>>> Numbers[0:10:-2]
[]
>>> Numbers[::-2]
[10, 8, 6, 4, 2]
>>> Numbers[5::-2]
[6, 4, 2]
>>> Numbers[:5:-2]
[10, 8]
>>>
2.2.3 Sequence Addition
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> ' Hello. ' + ' world '
' Hello. World
>>> ' Hello. ' +[1,2,3]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:cannot concatenate ' str ' and ' list ' objects
only sequences of the same type can be connected
2.2.4 Multiplication
>>> ' python '
' Pythonpythonpythonpythonpython '
>>> [42]*2
[42, 42]
None, empty list, and initialization
>>> sequence=[none]*10
>>> sequence
[None, None, none, none, none, none, none, none, none, none]
2.2.5 Membership
>>> users = [' Mlh ', ' foo ', ' Bar ']
>>> raw_input (' Enter your user name: ') in users
Enter your user Name:mlh
True
#字符串判断成员是, the detection is a character, as long as the character satisfies
>>> subject= ' $$$ Get rich now!!! $$$ '
>>> ' $$$ ' in subject
True
>>> ' $$ ' in subject
True
>>> ' $ ' in subject
True
>>> ' $ $ ' in subject
False
>>> database = [' alert ', ' 1234 '],[' Jones ', ' 9845 ']
>>> if [' Alert ', ' 1234 '] in Database:print ' Access granted '
...
Access granted
2.2.5 length, min value, maximum value
>>> numbers=[100,34,478]
>>> Len (Numbers)
3
>>> Max (Numbers)
478
>>> min (Numbers)
34
>>> Max (2,3)
3
>>> min (9,3,2,5)
2
2.3 List: Python's drudgery
The list is different from the tuple and the string: The list is mutable
2.3.1 List Function List function applies to all types of sequences, not just strings
>>> list (' Hello ')
[' H ', ' e ', ' l ', ' l ', ' O ']
2.3.2 Basic List Operations
Ways to change the list:
1, change the list: element assignment, cannot assign a value to an element that does not exist in a position
>>> x=[1,1,1]
>>> x[1]=2
>>> x
[1, 2, 1]
2, delete the element, in addition to delete the list, you can also delete the dictionary or other variables
>>> del X[1]
>>> x
[1, 1]
3, The Shard assignment, you can add the step length, the step can also be negative
>>> name=list (' Perl ')
>>> Name
[' P ', ' e ', ' r ', ' L ']
>>> name[2:]=list (' ar ')
>>> Name
[' P ', ' e ', ' a ', ' R ']
Application: can be replaced with unequal length
>>> name=list (' Perl ')
>>> name[1:]=list (' Ython ')
>>> Name
[' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
New values can be inserted in one place
>>> numbers=[1,5]
>>> numbers[1:1]=[2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
Can be used to delete elements
>>> numbers[1:4]=[]
>>> numbers
[1, 5]
Equivalent to: Del Numbers[1:4]
2.3.3 List Method
Method invocation: Object. Method (Parameter)
1, append does not return the value , the change is the original list
>>> lst=[1,2,3]
>>> Lst.append (4)
>>> LST
[1, 2, 3, 4]
2. Number of count statistics elements appearing in the list
>>> [' To ', ' is ', ' or ', ' not ', ' to ', ' is '].count (' to ')
2
>>> x=[[1,2],1,1,[2,1,[1,2]]
>>> X.count (1)
2
>>> X.count ([up])
1
3. Extend can expand the original list with the new list
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> A.extend (b)
>>> A
[1, 2, 3, 4, 5, 6]
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a+b
[1, 2, 3, 4, 5, 6]
>>> A
[1, 2, 3]
>>> a=a+b #并没有修改原来的列表
>>> A
[1, 2, 3, 4, 5, 6]
Use shard Copy to achieve the same effect, but with poor likelihood
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> A[len (a):]=b
>>> A
[1, 2, 3, 4, 5, 6]
4. Index
The index method is used to find the indexed position of the first occurrence of a value from a list
>>> knights=[' We ', ' is ', ' the ', ' knights ', ' who ', ' say ', ' ni ', ' Who ']
>>> Knights.index (' Who ')
4
>>> Knights[4]
' Who '
>>> knights.index (' Jack ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
ValueError:list.index (x): X not in List
5. Insert
The Insert method is used to insert the object into the list
>>> numbers=[1,2,3,5,6,7]
>>> Numbers.insert (3, ' four ')
>>> numbers
[1, 2, 3, ' Four ', 5, 6, 7]
Use shard assignment, but can read poor
>>> numbers=[1,2,3,5,6,7]
>>> numbers[3:3]=[' four '
>>> numbers
[1, 2, 3, ' Four ', 5, 6, 7]
6. Pop
The Pop method removes the specified element from the list (the last one by default) and returns the value of the element
>>> x=[1,2,3]
>>> X.pop ()
3
>>> x
[1, 2]
>>> X.pop (0)
1
>>> x
[2]
Last-in-first-out data structure: stack
Append () +pop ()
FIFO Data structure: queue
Insert (0,...) +pop () always add to the front
>>> x=[1,2,3]
>>> X.append (X.pop ())
>>> x
[1, 2, 3]
7. Remove does not return a value removes the first occurrence in the list
>>> x=[' to ', ' being ', ' or ', ' not ', ' to ', ' being ']
>>> x.remove (' be ')
>>> x
[' to ', ' or ', ' no ', ' to ', ' being ']
>>> x.remove (' Bee ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
ValueError:list.remove (x): X not in List
8, reverse not return value change the original list, reverse the original list
>>> x=[1,2,3]
>>> X.reverse ()
>>> x
[3, 2, 1]
>>> x=[1,2,3]
>>> list (reversed (x))
[3, 2, 1]
>>> Reversed (x)
<listreverseiterator Object at 0x7ffb167aaed0>
9. Sort has no return value
The sort method is used to sort the list at the original location, changing the original list
>>> x=[4,6,2,1,7,9]
>>> X.sort ()
>>> x
[1, 2, 4, 6, 7, 9]
The user needs to arrange a copy of the order, do not need to change the original list
The right approach:
>>> x=[4,6,2,1,7,9]
>>> y=x[:]
>>> Y.sort ()
>>> y
[1, 2, 4, 6, 7, 9]
>>> x
[4, 6, 2, 1, 7, 9]
>>> x=[4,6,2,1,7,9]
>>> y=sorted (x)
>>> y
[1, 2, 4, 6, 7, 9]
>>> x
[4, 6, 2, 1, 7, 9]
The wrong approach:
>>> x=[4,6,2,1,7,9]
>>> y=x
>>> Y.sort ()
>>> x
[1, 2, 4, 6, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
10. Advanced Sorting
>>> CMP (+)
-1
>>> CMP (2,1)
1
>>> CMP (+)
0
>>> numbers=[5,2,9,7]
>>> Numbers.sort (CMP)
>>> numbers
[2, 5, 7, 9]
>>> x=[4,6,2,1,7
Specify a sorted key
>>> x=[' aardvark ', ' abalone ', ' Acme ', ' Add ', ' aerate ']
>>> X.sort (Key=len)
>>> x
[' Add ', ' Acme ', ' aerate ', ' abalone ', ' aardvark ']
Specify reverse Sort
>>> x=[4,6,2,1,7,9]
>>> X.sort (reverse=true)
>>> x
[9, 7, 6, 4, 2, 1]
2.4: Tuples: Immutable sequences
>>>
(1, 2, 3)
>>> (a)
(1, 2, 3)
>>> ()
()
How do you implement a tuple of values? You must add a reading number
>>> (41)
41
>>> (41,)
(41,)
>>> 41
41
>>> 41,
(41,)
>>> (42)
126
>>> (40+2,)
(42, 42, 42)
2.4:tuple elements
Converting a sequence to a tuple
>>> tuple ([+])
(1, 2, 3)
>>> tuple (' abc ')
(' A ', ' B ', ' C ')
>>> tuple ((+))
(1, 2, 3)
2.4.2 operation of Primitive tuples
Creating tuples
>>> x=1,2,3
>>> X[1]
2
accessing tuples
>>> X[0:2]
(1, 2)
2.4. What is the meaning of the 3-tuple
1, tuples can be used as keys in the map-and the list is not
2, tuple as a number of intrinsic functions and the return value of the method exists
This article from "Small Fish Blog" blog, declined reprint!
Reading notes-The second edition of the Basic Python Tutorial-2nd Chapter list and tuples