高级搜索技术
复杂字典搜索策略
基于正则表达式的搜索
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
## 示例用法
data = {
"users": {
"admin_user": {"name": "John", "role": "admin"},
"manager_user": {"name": "Alice", "role": "manager"}
}
}
print(regex_dict_search(data, r'admin'))
函数式搜索技术
条件过滤
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
## 示例
complex_data = {
"departments": {
"engineering": {"size": 50, "budget": 100000},
"marketing": {"size": 30, "budget": 50000}
}
}
## 查找员工人数超过40的部门
large_departments = advanced_filter(
complex_data,
lambda dept: dept.get('size', 0) > 40
)
性能优化技术
graph TD
A[搜索策略] --> B{复杂度}
B -->|低| C[简单递归]
B -->|中| D[索引搜索]
B -->|高| E[优化算法]
索引搜索方法
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)
## 使用示例
indexed_data = IndexedDictionary({
"company": {
"employees": {
"engineering": {"count": 50}
}
}
})
print(indexed_data.search("company.employees.engineering.count"))
搜索性能比较
搜索方法 |
时间复杂度 |
空间复杂度 |
简单递归 |
O(n) |
O(d) |
基于正则表达式 |
O(n * m) |
O(k) |
索引搜索 |
O(1) |
O(n) |
高级过滤技术
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)
}
## 多个条件的示例
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
}
)
使用 LabEx 的关键考虑因素
- 选择合适的搜索策略
- 考虑内存和时间复杂度
- 实现健壮的错误处理
- 使用类型安全的搜索方法
通过掌握这些高级搜索技术,你将能够在 Python 项目中精确且高效地处理复杂的字典操作。