Merge pull request #196 from 30-seconds/covid-snippets

Add new snippets
This commit is contained in:
Isabelle Viktoria Maciohsek
2020-04-16 19:30:55 +03:00
committed by GitHub
6 changed files with 136 additions and 0 deletions

View File

@ -0,0 +1,25 @@
---
title: collect_dictionary
tags: dictionary,intermediate
---
Inverts a dictionary with non-unique hashable values.
Use `dictionary.items()` in combination with a loop to map the values of the dictionary to keys using `dictionary.setdefault()`, `list()` and `append()` to create a list for each one.
```py
def collect_dictionary(obj):
inv_obj = {}
for key, value in obj.items():
inv_obj.setdefault(value, list()).append(key)
return inv_obj
```
```py
ages = {
"Peter": 10,
"Isabel": 10,
"Anna": 9,
}
collect_dictionary(ages) # { 10: ["Peter", "Isabel"], 9: ["Anna"] }
```

22
snippets/find_key.md Normal file
View File

@ -0,0 +1,22 @@
---
title: find_key
tags: dictionary,intermediate
---
Returns the first key in the provided dictionary that has the given value.
Use `dictionary.items()` and `next()` to return the first key that has a value equal to `val`.
```py
def find_key(dict, val):
return next(key for key, value in dict.items() if value == val)
```
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
find_key(ages, 11) # "Isabel"
```

22
snippets/find_keys.md Normal file
View File

@ -0,0 +1,22 @@
---
title: find_keys
tags: dictionary,intermediate
---
Returns all keys in the provided dictionary that have the given value.
Use `dictionary.items()`, a generator and `list()` to return all keys that have a value equal to `val`.
```py
def find_keys(dict, val):
return list(key for key, value in dict.items() if value == val)
```
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 10,
}
find_keys(ages, 10) # [ "Peter", "Anna" ]
```

View File

@ -0,0 +1,22 @@
---
title: invert_dictionary
tags: dictionary,intermediate
---
Inverts a dictionary with unique hashable values.
Use `dictionary.items()` in combination with a list comprehension to create a new dictionary with the values and keys inverted.
```py
def invert_dictionary(obj):
return { value: key for key, value in obj.items() }
```
```py
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
invert_dictionary(ages) # { 10: "Peter", 11 "Isabel", 9: "Anna" }
```

View File

@ -0,0 +1,27 @@
---
title: merge_dictionaries
tags: dictionary,intermediate
---
Merges two or more dictionaries.
Create a new `dict()` and loop over `dicts`, using `dictionary.update()` to add the key-value pairs from each one to the result.
```py
def merge_dictionaries(*dicts):
res = dict()
for d in dicts:
res.update(d)
return res
```
```py
ages_one = {
"Peter": 10,
"Isabel": 11,
}
ages_two = {
"Anna": 9
}
merge_dictionaries(ages_one, ages_two) # { "Peter": 10, "Isabel": 11, "Anna": 9 }
```

18
snippets/to_dictionary.md Normal file
View File

@ -0,0 +1,18 @@
---
title: to_dictionary
tags: list,dictionary,intermediate
---
Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values.
The values of the first list need to be unique and hashable.
Use `zip()` in combination with a list comprehension to combine the values of the two lists, based on their positions.
```py
def to_dictionary(keys, values):
return {key:value for key, value in zip(keys, values)}
```
```py
to_dictionary(['a', 'b'], [1, 2]) # { a: 1, b: 2 }
```