|
List |
Tuple |
Dict |
Set |
| Characteristics |
Order, find speed gradually as the element increases |
Ordered \ cannot be modified |
Unordered, Find Fast, key cannot be duplicated |
Elements are not duplicated, unordered, and determine whether an element is fast in set |
| Create |
L =[' Michael ', ' Bob ', ' Tracy ' |
t = (' Adam ', ' Lisa ', ' Bart ')
t = (1,)
t = (' A ', ' B ', [' A ', ' B ']) |
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
} |
s = set ([' A ', ' B ', ' C ']) |
| Access |
L[0], l[-1] |
T[0], t[-1] |
Access via key
If ' Paul ' in D:
Print d[' Paul '
Or
D.get (' Paul ') |
x = '??? ' # user-entered string
If x in weekdays:
print ' Input ok '
Else
print ' input error ' |
| adding elements |
Tail add l.append (' Paul ')
Inserts the specified index position l.insert (0, ' Paul ') |
No |
d[' Paul '] = 72 |
S.add (4) |
| Delete Element |
Delete last element and return L.pop ()
Deletes an element of the specified index L.pop (2) |
No |
|
S.remove (4) |
| Replace element |
L[2] = ' Paul '
L[-1] = ' Paul ' |
No |
|
|
| The empty |
|
t = () |
|
|
| Number of elements |
|
|
Len (d) |
|
| Traverse |
|
|
For key in D:
... print Key |
For name in S:
... print Name |
| Slice |
Take the first 3 elements L[0:3] starting at index 0, until index 3, but not including index 3;
If the first index is 0, you can omit to write l[:3];
The third parameter indicates that each n takes one, on L[::2] |
|
|
|
| Iteration |
L = [' Adam ', ' Lisa ', ' Bart ', ' Paul ']
>>> for index, name in enumerate (L):
... print index, '-', name |
|
D = {' Adam ': +, ' Lisa ': $, ' Bart ': 59}
Print d.values ()
# [85, 95, 59]
For V in D.values ():
Print V
# 85
# 95
# 59
Or
D = {' Adam ': +, ' Lisa ': $, ' Bart ': 59}
Print d.itervalues ()
# <dictionary-valueiterator Object at 0x106adbb50>
For V in D.itervalues ():
Print V
# 85
# 95
# 59
Or for key, value in D.items ():
... print key, ': ', value
...
Lisa:85
Adam:95
bart:59 |
|