Transfer from http://www.cnblogs.com/BeginMan/p/3153842.html
First, Sequence type operator
1. Slice [] and [:]
2. Member relationship operator (in, not in)
1: S1 = [1,2,3,4,5,6,7]
2: s2 = [2,3,6]
3: s3 = []
4: In S1:
5: In S2:
6: s3.append (obj)
7:
8: Print S3
9: s4 = set (S1)-set (S2)
: Print S4 #set ([1, 4, 5, 7])
3. Connection operator (+)
+: Connect the same type on both sides, and create a new object after the connection, cannot add an element in, can only be a list object +list object.
1: print s1+s2 #[1, 2, 3, 4, 5, 6, 7, 2, 3, 6]
1: Note:
2: li=[1,2,3]
3: li.extend (' good '),
4: print Li outputs: [A/C, 'g ',' o 'o ',' d ']
5: If we switch to Li.append (' good '), then the output is [",' good '].
6: This is because extend () adds an entire list object, append () adds an element
Attention:
1: >>> li = [+]
2: >>> li +' new item '
3:
4: Traceback (most recent):
5: In <module>
6: Li + ' new item '
7: "str") to list
8: >>> li.extend (' item ')
9: >>> li
Ten: ' m ']
One: >>> li.append (' item ')
: >>> Li
: ' item ']
14:
4. Repeat operator (*)
Ii. list type built-in functions
1. List.append (obj): Add an object to the list obj
1: lis = [1,2,3,4,' a ',' B ',' C ',' a ',' aaa ',' e ',' great ']
2: lis.append (' object ')
3: print lis ' object ']
2, List.count (obj): Returns the number of times an object obj appears in the list
1: #print lis.count (' a ') #2
3. List.extend (SEQ): Add the contents of the sequence SEQ to the list
1: lis.extend (' seq ') ' Q ']
4, List.index (Obj,i=0,j=len (list)): Returns the K value of List[k]==obj, and the range of K is i<=k<j, otherwise throws ValueError exception
1: #print lis.index (' aaa ') #8
5. List.insert (index,obj): Inserts the object obj at index number (two parameters required)
1: Lis.insert (1,' Z ')
2: print lis ' great ']
6, List.pop (index =-1): Delete and return the object at the specified position, default is the last
1: print lis.pop () #great
2: print Lis.pop (1) #2
7. List.remove (obj): Remove object from list obj
1: lis = [1,2,3,4,' a ',' B ',' C ',' a ',' aaa ',' e ',' great ']
2: #lis. Remove (' value ')
3: #如果不存在则发生异常:
4: #Traceback (most recent):
5: # in <module>
6: # Lis.remove (' value ')
7: In list
8: lis.remove (' a ')
9: ' great ']
Ten: lis.remove (' e ')
One:
: Print Lis
8, List.reverse (): In-situ flip list
1: lis = [1,2,3,4,' a ',' B ',' C ',' a ',' aaa ',' e ',' great ']
2: lis.reverse ()
3: print lis #[' A ', 4, 3, 2, 1]
9, List.sort ()
1: lis = [1,2,3,4,' a ',' B ',' C ',' a ',' aaa ',' e ',' great ']
2: #lis. Sort (Cmp=none, Key=none, Reverse=false)
3: lis.sort (Cmp=none, Key=none, Reverse=true)
4: print lis #[' A ', 4, 3, 2, 1]
Note: The method of changing objects that can change the value of an object is not a return value.
Such as:
1: lis = [1,2,3,4,' a ',' B ',' C ',' a ',' aaa ',' e ',' great ']
2: print lis.extend (' MM ') #None
3: result = Lis.extend (' MM ')
4: print result #None
These operations perform operations on the list, meaning that the existing list contents are changed, but there is no return value. In contrast, the string method has a return value:
1: ' abc '. UPPER () #ABC
Where the built-in functions related to the sequence can have a return value:
1: print reversed (LIS) object at 0x0128dad0>
2: print sorted (LIS) ' great '
Third, sequence type function
1, Len ()
2, Max () min ()
3, sorted (), reversed (): Note that strings are sorted using a dictionary order instead of alphabetical order
4, enumerate (), Zip ()
See: http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html
5. SUM ()
6, List (), tuple ()
"Turn" Python Learning (7)-List