-
- What is a list?
- 1 Creating a list
- 2 access list and update list
- List-related actions
- Support for lists in built-in functions
- 1 CMP
- 2 Sequence type functions
- List built-in functions
- List application
1. What is a list?
Lists are also one of the sequences. Lists can hold any number of Python objects, and the list is a mutable type.
1.1 Creating a list
The list can be created using [], or by using the Factory method list ().
>>> t = list()>>> ‘list‘>>>> l = []>>> ‘list‘>>>> t == lTrue
1.2 Access list and update list
>>>t = List (' Furzoom ')>>>t[' F ',' u ',' R ',' Z ',' O ',' O ',' m ']>>>t[1]' u '>>>t[2] =' n '>>>t[' F ',' u ',' n ',' Z ',' O ',' O ',' m ']>>>T.append ('. ')>>>t[' F ',' u ',' n ',' Z ',' O ',' O ',' m ','. ']>>>delt[3]>>>t[' F ',' u ',' n ',' O ',' O ',' m ','. ']>>>delT>>>Ttraceback (most recent): File"<stdin>", line1,inch<module>nameerror:name' t ' is notDefined
2. List-related actions
Support comparison operation, slice [] or [:], in, not in, join operator +, repeat operation.
If possible, use the List.extend () method instead of the connection operator.
The list also supports very important list resolution operations.
>>> forin xrange(10)][0123456789]
3. Built-in function support for lists 3.1 CMP ()
Comparison principle:
- Compares the elements of a two list.
- If the element being compared is of the same type, the value is compared and the result is returned.
- If two elements are not of the same type, check whether they are numbers.
3.1 If it is a number, perform the necessary number to force the type conversion, and then compare.
3.2 If one of the elements is a number, the other party's element is large.
3.3 Otherwise, the alphabetical order of the type name is compared.
- If there is a list that reaches the end first, the other longer list is larger.
- If all two lists reach the end and all elements are equal, 0 is returned.
3.2 Sequence type functions
- Len ()
- Max ()
- Min ()
- Sorted ()
- Reversed ()
- Enumerate ()
- Zip ()
- SUM ()
- List ()
- Tuple ()
4. List built-in functions
- List.append (x)
- List.extend (x)
- List.count (x)
- List.index (x[, start[, end]])
- List.insert (index, X)
- List.pop ([index])
- List.remove (x)
- List.remove ()
- List.sort ([cmp[, key[, reverse]])
5. List Apply 5.1 stacks
#!/usr/bin/env python#-*-Coding:utf-8-*-stack = [] def pushit():Stack.append (Raw_input (' Enter New string: '). Strip ()) def popit(): ifLen (stack) = =0:Print ' cannot pop from an empty stack! ' Else:Print ' Removed [', ' Stack.pop () ','] ' def viewstack(): PrintStackcmds = {' u ': Pushit,' O ': Popit,' V ': Viewstack} def showmenu():PR ="" " p (U) SH p (O) p (V) iew (Q) uit Enter choice: " " while True: while True:Try: Choice = raw_input (pr). Strip () [0].lower ()except(Eoferror, Keyboardinterrupt, indexerror): choice =' Q ' Print ' \nyou picked: [%s] '% ChoiceifChoice not inch ' Uovq ':Print ' Invalid option, try Again ' Else: Break ifChoice = =' Q ': BreakCmds[choice] ()if__name__ = =' __main__ ': ShowMenu ()
The following example runs:
P (U) SH p (O) p (V) iew (Q) uitEnterChoice:uyou picked: [u]EnterNew String:python P (U) SH p (O) p (V) iew (Q) uitEnterChoice:uyou picked: [u]EnterNew String:is P (U) SH p (O) p (V) iew (Q) uitEnterChoice:uyou picked: [u]EnterNew string:cool! P (U) SH p (O) p (V) iew (Q) uitEnterChoice:vyou picked: [v][' Python ',' is ',' cool! '] P (U) SH p (O) p (V) iew (Q) uitEnterChoice:oyou picked: [O]removed[' cool! '] P (U) SH p (O) p (V) iew (Q) uitEnterChoice:oyou picked: [O]removed[' is '] P (U) SH p (O) p (V) iew (Q) uitEnterChoice:oyou picked: [O]removed[' Python '] P (U) SH p (O) p (V) iew (Q) uitEnterChoice:oyou picked: [O]cannot pop from an empty stack! P (U) SH p (O) p (V) iew (Q) uitEnterChoice: ^DYou picked: [Q]
5.2 Queues
#!/usr/bin/env python#-*-Coding:utf-8-*-Queue = [] def EnQ():Queue.append (Raw_input (' Enter New string: '). Strip ()) def DeQ(): ifLen (queue) = =0:Print ' cannot pop from an empty queue! ' Else:Print ' Removed [', ' Queue.pop (0)`,'] ' def viewq(): PrintQueuecmds = {' E ': EnQ,' d ': DeQ,' V ': Viewq} def showmenu():PR ="" " (E) nqueue (D) equeue (V) iew (Q) uit Enter choice: " " while True: while True:Try: Choice = raw_input (pr). Strip () [0].lower ()except(Eoferror, Keyboardinterrupt, indexerror): choice =' Q ' Print ' \nyou picked: [%s] '% ChoiceifChoice not inch ' Edvq ':Print ' Invalid option, try Again ' Else: Break ifChoice = =' Q ': BreakCmds[choice] ()if__name__ = =' __main__ ': ShowMenu ()
The following example runs:
(E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:eyou picked: [E]EnterNew string:bring Out (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:eyou picked: [E]EnterNew String:your dead! (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:vyou picked: [v][' Bring out ',' Your dead! '] (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:dyou picked: [d]removed[' Bring out '] (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:dyou picked: [d]removed[' Your dead! '] (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice:dyou picked: [D]cannot pop from an empty queue! (E) Nqueue (D) Equeue (V) iew (Q) uitEnterChoice: ^DYou picked: [Q]
Python Sequence--list