Applies a given function to the values of a nested dictionary
Tested against Windows 10 / Python 3.10 / Anaconda
pip install functionapplydict
apply_function_dict(d: dict, fu=lambda keys, item, d: item):
Apply a given function to the values of a nested dictionary and return a modified dictionary.
Parameters:
- d (dict): The input dictionary to be modified.
- fu (function): The function to be applied to each value in the dictionary.
- keys (list): The list of keys leading to the current value.
- item (any): The current value in the dictionary.
- d (dict): The input dictionary.
Returns:
- dict: A new dictionary with the same structure as the input, but with modified values.
Example:
from functionapplydict import apply_function_dict
child1 = {"name": "Emil", "year": 2004}
child2 = {"name": "Tobias", "year": 2007}
child3 = {"name": "Linus", "year": 2011}
myfamily = {"child1": child1, "child2": child2, "child3": child3}
def function1(keys, item, d):
if isinstance(item, int):
item = item * 8
return item
dnew = apply_function_dict(d=myfamily, fu=function1)
dnew2 = apply_function_dict(d=myfamily, fu=lambda keys, item, d: str(item))
ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
dnew3 = apply_function_dict(d=ditest, fu=lambda keys, item, d: sum(item))
dnew4 = apply_function_dict(d=ditest, fu=lambda keys, item, d: str(item))
print(dnew)
print(dnew2)
print(dnew3)
print(dnew4)
# {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
# {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
# {1: {2: 122}, 2: {24: 566}} # sum can be applied to lists
# {1: {2: '[4, 56, 2, 4, 56]'}, 2: {24: '[4, 56, 2, 4, 56, 444]'}} # the whole list is converted to a string
apply_function_dict_deep(d: dict, fu=lambda keys, item, d: item):
Apply a given function to the values of a nested dictionary deeply and return a modified dictionary.
Parameters:
- d (dict): The input dictionary to be modified.
- fu (function): The function to be applied to each value in the dictionary.
- keys (list): The list of keys leading to the current value.
- item (any): The current value in the dictionary.
- d (dict): The input dictionary.
Returns:
- dict: A new dictionary with the same structure as the input, but with modified values.
Example:
from functionapplydict import apply_function_dict_deep
child1 = {"name": "Emil", "year": 2004}
child2 = {"name": "Tobias", "year": 2007}
child3 = {"name": "Linus", "year": 2011}
myfamily = {"child1": child1, "child2": child2, "child3": child3}
def function1(keys, item, d):
if isinstance(item, int):
item = item * 8
return item
dnew = apply_function_dict_deep(d=myfamily, fu=function1)
dnew2 = apply_function_dict_deep(d=myfamily, fu=lambda keys, item, d: str(item))
ditest = {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}}
dnew3 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: sum(item))
dnew4 = apply_function_dict_deep(d=ditest, fu=lambda keys, item, d: str(item))
print(dnew)
print(dnew2)
print(dnew3)
print(dnew4)
# {'child1': {'name': 'Emil', 'year': 16032}, 'child2': {'name': 'Tobias', 'year': 16056}, 'child3': {'name': 'Linus', 'year': 16088}}
# {'child1': {'name': 'Emil', 'year': '2004'}, 'child2': {'name': 'Tobias', 'year': '2007'}, 'child3': {'name': 'Linus', 'year': '2011'}}
# {1: {2: [4, 56, 2, 4, 56]}, 2: {24: [4, 56, 2, 4, 56, 444]}} # no change, because sum can't be applied to int
# {1: {2: ['4', '56', '2', '4', '56']}, 2: {24: ['4', '56', '2', '4', '56', '444']}} # each int is converted to a string, but not the whole list
Metadata
Release files for functionapplydict 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| functionapplydict-0.10.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| functionapplydict-0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.8 kB
Release files / functionapplydict-0.10.tar.gz
| Download URL | functionapplydict-0.10.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0efbc66184926ee73c9687bcca2ec192a0b1bb19ebf37d453dc712dafc1bb38d
|
|
BLAKE2b-256 checksum How to use checksums |
02e3fb60c527345842065a3d86da585762cec408273d332b1196b615a6c947c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Release files / functionapplydict-0.10-py3-none-any.whl
| Download URL | functionapplydict-0.10-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
edca94fa3e512e1b9f4f7c89e1678dace09fbbc80e0d6a0eea6318a91cba4f36
|
|
BLAKE2b-256 checksum How to use checksums |
c320ecb9cfd8be56c9c0116bae1030bd5e51017d9098df1705da19f857beca35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|