Advanced Search Techniques
Complex Dictionary Searching Strategies
Regex-Based Search
import re
def regex_dict_search(dictionary, pattern):
results = {}
def search(current_dict, path=''):
for key, value in current_dict.items():
current_path = f"{path}.{key}" if path else key
if re.search(pattern, str(key)) or re.search(pattern, str(value)):
results[current_path] = value
if isinstance(value, dict):
search(value, current_path)
search(dictionary)
return results
## Example usage
data = {
"users": {
"admin_user": {"name": "John", "role": "admin"},
"manager_user": {"name": "Alice", "role": "manager"}
}
}
print(regex_dict_search(data, r'admin'))
Functional Search Techniques
Conditional Filtering
def advanced_filter(dictionary, condition):
results = {}
def deep_filter(current_dict):
for key, value in current_dict.items():
if isinstance(value, dict):
if condition(value):
results[key] = value
deep_filter(value)
deep_filter(dictionary)
return results
## Example
complex_data = {
"departments": {
"engineering": {"size": 50, "budget": 100000},
"marketing": {"size": 30, "budget": 50000}
}
}
## Find departments with more than 40 employees
large_departments = advanced_filter(
complex_data,
lambda dept: dept.get('size', 0) > 40
)
graph TD
A[Search Strategy] --> B{Complexity}
B -->|Low| C[Simple Recursive]
B -->|Medium| D[Indexed Search]
B -->|High| E[Optimized Algorithm]
Indexed Search Method
class IndexedDictionary:
def __init__(self, data):
self.data = data
self.index = {}
self._build_index()
def _build_index(self):
def index_recursive(current_dict, path=''):
for key, value in current_dict.items():
current_path = f"{path}.{key}" if path else key
self.index[current_path] = value
if isinstance(value, dict):
index_recursive(value, current_path)
index_recursive(self.data)
def search(self, path):
return self.index.get(path)
## Usage example
indexed_data = IndexedDictionary({
"company": {
"employees": {
"engineering": {"count": 50}
}
}
})
print(indexed_data.search("company.employees.engineering.count"))
Search Method |
Time Complexity |
Space Complexity |
Simple Recursive |
O(n) |
O(d) |
Regex-Based |
O(n * m) |
O(k) |
Indexed Search |
O(1) |
O(n) |
Advanced Filtering Techniques
def multi_condition_search(dictionary, conditions):
def match_all_conditions(item):
return all(
condition(item.get(key))
for key, condition in conditions.items()
)
return {
key: value
for key, value in dictionary.items()
if match_all_conditions(value)
}
## Example with multiple conditions
data = {
"products": {
"laptop": {"price": 1000, "stock": 50},
"smartphone": {"price": 500, "stock": 20}
}
}
filtered_products = multi_condition_search(
data['products'],
{
'price': lambda x: x > 700,
'stock': lambda x: x > 30
}
)
Key Considerations with LabEx
- Choose appropriate search strategy
- Consider memory and time complexity
- Implement robust error handling
- Use type-safe searching methods
By mastering these advanced search techniques, you'll be able to handle complex dictionary operations with precision and efficiency in your Python projects.