Python_Comprehend_Daily
2017-12-21
- argparse
- curses
- getch(): window.getch()/stdscr.getch()
list/tuple
- tuple:
1
2
3
4
5tuple1 = ([1, 2, 3],(4, 5, 6), (7, 8, 9))
tuple2 = ((1, 2, 3),(4, 5, 6), (7, 8, 9))
type(tuple1[0]) = list
type(tuple1[1]) = tuple
type(list(tuple1[1])) = list
- tuple:
defaultdict
- source: collections
notes
TypeError: first argument must be callable or None
- For a
defaultdict, the default value is usually not really a value, it is a factory: a method that generates a new value. Example:
Wrong:
1
2lst = list(range(0, 5))
d = defaultdict(lst)the terminal will report an error like this:
1
TypeError: first argument must be callable or None
Right:
1
d = defaultdict(lambda: list(range(0, 5)))
Extend: the codes below will make all default values(like d[1]/d[2]) reference the same list
Example:
Wrong:
1
2
3
4
5
6
7>>> val = list(range(0, 5))
>>> d = defaultdict(lambda: val)
>>> d[1]
[0, 1, 2, 3, 4]
>>> d[1].append(14)
>>> d[2]
[0, 1, 2, 3, 4, 14]Right:
1
2
3
4
5
6
7>>> val = lambda: list(range(0, 5))
>>> d = defaultdict(val)
>>> d[1]
[0, 1, 2, 3, 4]
>>> d[1].append(14)
>>> d[2]
[0, 1, 2, 3, 4]
Reason:
- For a