Add a new method to return count of values and self values
This commit is contained in:
parent
d3e0ea564b
commit
2e8098ef62
3 changed files with 75 additions and 17 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -8,3 +8,4 @@ nested_lookup.egg-info/*
|
||||||
.cache
|
.cache
|
||||||
.vscode
|
.vscode
|
||||||
git_commit.sh
|
git_commit.sh
|
||||||
|
venv
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,6 @@ from .nested_lookup import (
|
||||||
get_all_keys,
|
get_all_keys,
|
||||||
get_occurrence_of_key,
|
get_occurrence_of_key,
|
||||||
get_occurrence_of_value,
|
get_occurrence_of_value,
|
||||||
|
get_occurrence_of_value_with_self_value
|
||||||
)
|
)
|
||||||
from .lookup_api import nested_update, nested_delete, nested_alter
|
from .lookup_api import nested_update, nested_delete, nested_alter
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ from six import iteritems
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
values_list = []
|
||||||
|
|
||||||
|
|
||||||
def nested_lookup(key, document, wild=False, with_keys=False):
|
def nested_lookup(key, document, wild=False, with_keys=False):
|
||||||
"""Lookup a key in a nested document, return a list of values"""
|
"""Lookup a key in a nested document, return a list of values"""
|
||||||
if with_keys:
|
if with_keys:
|
||||||
|
|
@ -80,6 +83,47 @@ def get_occurrence_of_key(dictionary, key):
|
||||||
return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
|
return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
|
||||||
|
|
||||||
|
|
||||||
|
def get_occurrence_of_value_with_self_value(items: list, value: any) -> dict:
|
||||||
|
"""
|
||||||
|
Method to get occurrence of a value in a nested list of dictionary
|
||||||
|
|
||||||
|
Args:
|
||||||
|
items: list of dictionary: Nested dictionary
|
||||||
|
value: Value to search for the occurrences
|
||||||
|
|
||||||
|
Return:
|
||||||
|
Dict where the key is the value arg and his value is a new
|
||||||
|
dict with occurrences and values
|
||||||
|
"""
|
||||||
|
occurrences = {}
|
||||||
|
occurrence = 0
|
||||||
|
value_list = []
|
||||||
|
|
||||||
|
for item in items:
|
||||||
|
occurrence_result, values = _get_occurrence_with_values(dictionary=item, item="value", keyword=value)
|
||||||
|
occurrence = occurrence + occurrence_result
|
||||||
|
if occurrence_result:
|
||||||
|
value_list.extend(values)
|
||||||
|
|
||||||
|
occurrences[value] = {
|
||||||
|
'occurrences': occurrence,
|
||||||
|
'values': value_list
|
||||||
|
}
|
||||||
|
|
||||||
|
return occurrences
|
||||||
|
|
||||||
|
|
||||||
|
def _get_occurrence_with_values(dictionary, item, keyword):
|
||||||
|
occurrence = [0]
|
||||||
|
|
||||||
|
result_recursion = recursion(dictionary, item, keyword, occurrence, True)
|
||||||
|
|
||||||
|
global values_list
|
||||||
|
values_list = []
|
||||||
|
|
||||||
|
return occurrence[0], result_recursion
|
||||||
|
|
||||||
|
|
||||||
def get_occurrence_of_value(dictionary, value):
|
def get_occurrence_of_value(dictionary, value):
|
||||||
"""
|
"""
|
||||||
Method to get occurrence of a value in a nested dictionary
|
Method to get occurrence of a value in a nested dictionary
|
||||||
|
|
@ -93,6 +137,31 @@ def get_occurrence_of_value(dictionary, value):
|
||||||
return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
|
return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
|
||||||
|
|
||||||
|
|
||||||
|
def recursion(dictionary, item, keyword, occurrence, with_values=False):
|
||||||
|
|
||||||
|
global values_list
|
||||||
|
|
||||||
|
if item == "key":
|
||||||
|
if dictionary.get(keyword) is not None:
|
||||||
|
occurrence[0] += 1
|
||||||
|
elif keyword in list(dictionary.values()):
|
||||||
|
occurrence[0] += list(dictionary.values()).count(keyword)
|
||||||
|
if with_values:
|
||||||
|
values_list.append(dictionary)
|
||||||
|
for key, value in iteritems(dictionary):
|
||||||
|
if isinstance(value, dict):
|
||||||
|
recursion(value, item, keyword, occurrence, with_values)
|
||||||
|
elif isinstance(value, list):
|
||||||
|
for list_items in value:
|
||||||
|
if hasattr(list_items, "items"):
|
||||||
|
recursion(list_items, item, keyword, occurrence, with_values)
|
||||||
|
elif list_items == keyword:
|
||||||
|
occurrence[0] += 1 if item == "value" else 0
|
||||||
|
|
||||||
|
if values_list:
|
||||||
|
return values_list
|
||||||
|
|
||||||
|
|
||||||
def _get_occurrence(dictionary, item, keyword):
|
def _get_occurrence(dictionary, item, keyword):
|
||||||
"""
|
"""
|
||||||
Method to get occurrence of a key or value in a nested dictionary
|
Method to get occurrence of a key or value in a nested dictionary
|
||||||
|
|
@ -105,22 +174,9 @@ def _get_occurrence(dictionary, item, keyword):
|
||||||
Number of occurrence of the given keyword in the dict
|
Number of occurrence of the given keyword in the dict
|
||||||
"""
|
"""
|
||||||
occurrence = [0]
|
occurrence = [0]
|
||||||
|
recursion(dictionary, item, keyword, occurrence)
|
||||||
|
|
||||||
def recrusion(dictionary):
|
global values_list
|
||||||
if item == "key":
|
values_list = []
|
||||||
if dictionary.get(keyword) is not None:
|
|
||||||
occurrence[0] += 1
|
|
||||||
elif keyword in list(dictionary.values()):
|
|
||||||
occurrence[0] += list(dictionary.values()).count(keyword)
|
|
||||||
for key, value in iteritems(dictionary):
|
|
||||||
if isinstance(value, dict):
|
|
||||||
recrusion(dictionary=value)
|
|
||||||
elif isinstance(value, list):
|
|
||||||
for list_items in value:
|
|
||||||
if hasattr(list_items, "items"):
|
|
||||||
recrusion(dictionary=list_items)
|
|
||||||
elif list_items == keyword:
|
|
||||||
occurrence[0] += 1 if item == "value" else 0
|
|
||||||
|
|
||||||
recrusion(dictionary=dictionary)
|
|
||||||
return occurrence[0]
|
return occurrence[0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue