Python learns list notes
Python learns list notes
List (list) is the most frequently used data type in Python
Support for characters, numbers, strings and even lists (so-called nesting)
1. Definition:
List = [1, 3, 4, 5, 7, 4, 5, 7, 4, 4, 5, 4, 5, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 4, 5, 4, 4, 5, 5, 4, 4, 4, 4, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4, 4
2. Increase from the last column:
List.append ("your are good!")
Display:
Direct input: list
Display: [1,3,4,5, 'goog',' well', 777, 'your are goodwill']
3. Add a column from the middle such as:
Use: insert function:
The method of use is as follows:
> list.insert.
> list
[1,3,4,5, 'goog',' liwen', 'well',' liwen', 'liwen', 777,' your are goodwill']
4. Delete one:
Use del
Such as:
> list
[1,3,4,5, 'goog',' liwen', 'well',' liwen', 'liwen', 777,' your are goodwill']
> del list [1] # Delete the first subscript value
> list
[1,4,5, 'goog',' liwen', 'well',' liwen', 'liwen', 777,' your are goodwill']
5. Traverse all values:
List = [1,4,5, 'goog',' liwen', 'well',' liwen', 'liwen', 777,' your are goodwill']
Use the for loop to iterate through:
For i in list:
Print i
6. Use the len function to view the list length
List = [1,4,5, 'goog',' liwen', 'well',' liwen', 'liwen', 777,' your are goodwill']
Print len (list)
Print'='
A = 'ddddddddddddddddddd'
Print len (a)
ten
=
nineteen
=
7. Calculation: statistics 5 appeared several times:
C = [1, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 6, 9]
Ncount = 0
For i in c:
If I = = 5:
Ncount = ncount + 1
Print ncount
Results:
six
= ncount=
Even and odd numbers:
Range (1, 101, 2) Odd
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63,65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
Range (2, 101, 2) even numbers
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
Add:
1 、
#! / usr/bin/env python
#-*-coding: utf-8-*-
If _ _ name__ = ='_ _ main__':
List = ['html',' js', 'css',' python']
# method 1
Print u 'traversal list method 1VR'
For i in list:
Print (u "serial number:% s value:% s"% (list.index (I) + 1, I))
Print u'\ ntraversal list method 2vl'
# method 2
For i in range (len (list)):
Print (u "serial number:% s value:% s"% (I + 1, list [I]))
# method 3
Print u'\ ntraversal list method 3VR'
For I, val in enumerate (list):
Print (u "serial number:% s value:% s"% (I + 1, val))
# method 3
Print u'\ nTraverse list method 3 (sets the initial position of the traversal start, only changes the starting sequence number):'
For I, val in enumerate (list, 2):
Print (u "serial number:% s value:% s"% (I + 1, val))