Applies a given function to the values of a nested dictionary
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
functionapplydict-0.10.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for functionapplydict-0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edca94fa3e512e1b9f4f7c89e1678dace09fbbc80e0d6a0eea6318a91cba4f36 |
|
MD5 | 2743bbece5112990f7db744e875b00f8 |
|
BLAKE2b-256 | c320ecb9cfd8be56c9c0116bae1030bd5e51017d9098df1705da19f857beca35 |