Browse Source

Update

pull/1/head
Jure Šorn 7 years ago
parent
commit
abfabfe8a9
1 changed files with 27 additions and 21 deletions
  1. 48
      README.md

48
README.md

@ -16,9 +16,12 @@ List
<list>.extend(<list>) <list>.extend(<list>)
<list>.sort() <list>.sort()
<list>.reverse() <list>.reverse()
```
```python
sum(<list>) sum(<list>)
sorted_by_second = sorted(<list>, key=lambda tup: tup[1]) sorted_by_second = sorted(<list>, key=lambda tup: tup[1])
[item for sublist in <list> for item in sublist] # Flattens List
[item for sublist in <list> for item in sublist] # Flattens List.
``` ```
Dictionary Dictionary
@ -28,9 +31,12 @@ Dictionary
<dict>.get(key, default) <dict>.get(key, default)
<dict>.setdefault(key, default) <dict>.setdefault(key, default)
<dict>.update(<dict>) <dict>.update(<dict>)
collections.defaultdict(<type>)
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
```
```python
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.
``` ```
#### Counter #### Counter
@ -50,7 +56,7 @@ Set
<set>.union(<set>) <set>.union(<set>)
<set>.intersection(<set>) <set>.intersection(<set>)
<set>.difference(<set>) <set>.difference(<set>)
<frozenset> # Is hashable and can be used as key in dictionary
<frozenset> # Is hashable and can be used as key in dictionary.
``` ```
Range Range
@ -58,7 +64,7 @@ Range
```python ```python
range(to_exclusive) range(to_exclusive)
range(from_inclusive, to_exclusive) range(from_inclusive, to_exclusive)
range(from_inclusive, to_exclusive, step_size) # Negative step for backward
range(from_inclusive, to_exclusive, step_size) # Negative step_size for backward
``` ```
Enumerate Enumerate
@ -98,16 +104,17 @@ def step(start, step):
while True: while True:
yield start yield start
start += step start += step
```
```python
stepper = step(10, 2) stepper = step(10, 2)
next(stepper) # -> 10 (, 12, 14, ...)
next(stepper) # 10 (, 12, 14, ...)
``` ```
Type Type
---- ----
```python ```python
type(<el>) # is int/str/set/list/...
type(<el>) # int/str/set/list/...
``` ```
```python ```python
import numbers import numbers
@ -139,17 +146,17 @@ re.search(<regex>, text)
### Format ### Format
```python ```python
'{}'.format(<el>)
'{}'.format(<el> [, <el>, ...])
``` ```
```python ```python
{:min_width} # -> '<el> '
{:>min_width} # -> ' <el>'
{:^min_width} # -> ' <el> '
{:_min_width} # -> '<el>____'
{:.max_width} # -> '<e>'
{:max_widht.min_width} # -> ' <e>'
{:max_width.no_of_decimalsf} # -> ' 3.14'
{:min_width} # '<el> '
{:>min_width} # ' <el>'
{:^min_width} # ' <el> '
{:_min_width} # '<el>____'
{:.max_width} # '<e>'
{:max_widht.min_width} # ' <e>'
{:max_width.no_of_decimalsf} # ' 3.14'
``` ```
```python ```python
@ -198,16 +205,15 @@ func(*args, **kwargs) # Is same as
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
------ ------
### Lambda ### Lambda
```python ```python
lambda <argument1>, <argument2>: <return value>
lambda: <return value>
lambda <argument1>, <argument2>: <return_value>
lambda: <return_value>
``` ```
### Comprehension ### Comprehension

Loading…
Cancel
Save