From 1264a7ccec1946fd2eb7b3d4c6db5255e36c6cf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 25 Mar 2018 10:52:13 +0200 Subject: [PATCH] Update readme --- README.md | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ac569c1..2817441 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Comprehensive Python Cheatsheet Main ---- -```python +```javascript if __name__ == '__main__': main() ``` @@ -34,8 +34,8 @@ Dictionary ``` ```python -collections.defaultdict() # Creates a dictionary with default values. -dict(zip(keys, values)) # Initiates a dict from two lists. +collections.defaultdict() # Creates a dictionary with default values. +dict(zip(keys, values)) # Initiates a dict from two lists. {k: v for k, v in .iteritems() if k in } # Filters a dict by keys. ``` @@ -79,8 +79,12 @@ Named Tuple ----------- ```python >>> TestResults = collections.namedtuple('TestResults', ['filed', 'attempted']) ->>> TestResults(1, 2) +>>> a = TestResults(1, attempted=2) TestResults(filed=1, attempted=2) +>>> a.filed +1 +>>> getattr(a, 'attempted') +2 ``` Iterator @@ -196,8 +200,9 @@ Datetime ```python import datetime 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 @@ -212,7 +217,8 @@ func(*args, **kwargs) 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 @@ -334,8 +340,8 @@ class (enum.Enum): ```python Cutlery = Enum('Cutlery', ['knife', 'fork', 'spoon']) -list() # == [, , ...] -random.choice(list()) # == random +list() # == [, , ...] +random.choice(list()) # == random ``` ### Copy @@ -464,7 +470,8 @@ lock.release() 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. ```python @@ -517,14 +524,13 @@ from itertools import * ### Groupby ```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"])} -{'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