1. Data structure:
List
Tuples and Sequences
Sets
Dictionary
2. Methods of list object:
list.append(x)
list.extend(L)
list.insert(i,x) You can test the position.
list.remove(x)
list.pop(i) remove the item at the given position and return it.
list.pop() by default i=len(list), it's the last item
list.clear()
list.index(x) returns the index in the list of the item whose value is x
list.count(x)
list.sort()
list.reverse()
list.copy()
3. Methods that have no return values printed return None
4. Lists can be used as stacks by using the methods list.pop() and list.append.
5. Using lists as queues is not efficient!( Because all of the other elements have to be shifted by one)
collections.deque can do some help
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry") # Terry arrives
>>> queue.append("Graham") # Graham arrives
>>> queue.popleft() # The first to arrive now leaves
'Eric'
>>> queue.popleft() # The second to arrive now leaves
'John'
>>> queue # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])
6. concise 简洁的
7. List comprehensions
a_list=list(range(5))
[(x,x**2) for x in a_list]
8. Nested List Comprehension
>>> matrix = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
9. del a[2:4]
del a
10. A tuple consists of a number of values separated by commas.
t=11,2,23,'dbvsxd'
empty_tuple=()
singleton='Hello',
immutable
11. Sets
No order, no duplication.
empty_set=set()
empty_dictionary={}
a=set('alacazam')
b=set('fasfsd')
a-b
a|b letters in either a or b
a&b
a^b letters in a or b but not both=a|b-a&b
12. Dictionary indexed by keys
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
13.
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
14.
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
15.
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}
16.
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for k, v in knights.items():
print(k, v)
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
17.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
18.
>>> for i in reversed(range(1, 10, 2)):
... print(i)
19. How to delete the duplicate words?Here we go!
list->set->list
20. assignment cannot occur inside expression
21.