Browse Source

Update readme

pull/1/head
Jure Šorn 6 years ago
parent
commit
1264a7ccec
1 changed files with 23 additions and 17 deletions
  1. 40
      README.md

40
README.md

@ -3,7 +3,7 @@ Comprehensive Python Cheatsheet
Main Main
---- ----
```python
```javascript
if __name__ == '__main__': if __name__ == '__main__':
main() main()
``` ```
@ -34,8 +34,8 @@ Dictionary
``` ```
```python ```python
collections.defaultdict(<type>) # Creates a dictionary with default values.
dict(zip(keys, values)) # Initiates a dict from two lists.
collections.defaultdict(<type>) # Creates a dictionary with default values.
dict(zip(keys, values)) # Initiates a dict from two lists.
{k: v for k, v in <dict>.iteritems() if k in <list>} # Filters a dict by keys. {k: v for k, v in <dict>.iteritems() if k in <list>} # Filters a dict by keys.
``` ```
@ -79,8 +79,12 @@ Named Tuple
----------- -----------
```python ```python
>>> TestResults = collections.namedtuple('TestResults', ['filed', 'attempted']) >>> TestResults = collections.namedtuple('TestResults', ['filed', 'attempted'])
>>> TestResults(1, 2)
>>> a = TestResults(1, attempted=2)
TestResults(filed=1, attempted=2) TestResults(filed=1, attempted=2)
>>> a.filed
1
>>> getattr(a, 'attempted')
2
``` ```
Iterator Iterator
@ -196,8 +200,9 @@ Datetime
```python ```python
import datetime import datetime
now = datetime.datetime.now() now = datetime.datetime.now()
now.strftime('%Y%m%d')
now.strftime('%Y%m%d%H%M%S')
now.month # 3
now.strftime('%Y%m%d') # 20180315
now.strftime('%Y%m%d%H%M%S') # 20180315002834
``` ```
Arguments Arguments
@ -212,7 +217,8 @@ func(*args, **kwargs)
func(1, 2, x=3, y=4, z=5) func(1, 2, x=3, y=4, z=5)
``` ```
#### "*" is the splat operator, that takes a list as input, and expands it into actual positional arguments in the function call.
#### "*" is the splat operator, that takes a list as input, and expands it into
actual positional arguments in the function call.
Inline Inline
@ -334,8 +340,8 @@ class <enum_name>(enum.Enum):
```python ```python
Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon']) Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon'])
list(<enum_name>) # == [<enum1>, <enum2>, ...]
random.choice(list(<enum_name>)) # == random <enum>
list(<enum_name>) # == [<enum1>, <enum2>, ...]
random.choice(list(<enum_name>)) # == random <enum>
``` ```
### Copy ### Copy
@ -464,7 +470,8 @@ lock.release()
Itertools Itertools
--------- ---------
Every function returns a generator and can accept any collection. If you want to print an output of generator, as in examples, you need to pass it to _list()_
Every function returns a generator and can accept any collection. If you want to
print an output of generator, as in examples, you need to pass it to _list()_
function. function.
```python ```python
@ -517,14 +524,13 @@ from itertools import *
### Groupby ### Groupby
```python ```python
>>> {k: list(v) for k, v in groupby("aabbbc")}
{'a': ['a', 'a'], 'b': ['b', 'b', 'b'], 'c': ['c']}
```
```python
>>> a = [{"id": 1, "name": "bob"}, {"id": 2, "name": "bob"}, {"id": 3, "name": "peter"}]
>>> a = [{"id": 1, "name": "bob"},
{"id": 2, "name": "bob"},
{"id": 3, "name": "peter"}]
>>> {k: list(v) for k, v in groupby(a, key=lambda x: x["name"])} >>> {k: list(v) for k, v in groupby(a, key=lambda x: x["name"])}
{'bob': [{'id': 1, 'name': 'bob'}, {'id': 2, 'name': 'bob'}], 'peter': [{'id': 3, 'name': 'peter'}]}
{'bob': [{'id': 1, 'name': 'bob'},
{'id': 2, 'name': 'bob'}],
'peter': [{'id': 3, 'name': 'peter'}]}
``` ```
### Islice ### Islice

Loading…
Cancel
Save