Python 如何创建另一个包含字符串的字典
我正在试图提取包含字符串的词典 字典在下面Python 如何创建另一个包含字符串的字典,python,dictionary,Python,Dictionary,我正在试图提取包含字符串的词典 字典在下面 [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" }, { "id": &qu
[ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" },
{ "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
{ "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
我正在努力提取
业务领域是
,并且工程领域
是测试人员还是开发人员以及角色
为名称
和L1
是位置
NY
def get_set(d,field):
return {d[field]} if isinstance(d[field], str) else set(d[field])
# we use this to filter
def validate(d):
if 'Role' in d or `businessArea` in d or `Designation` in d or `Location` in d :
return get_set(d,'Role').intersection({'Developer','Tester'}) and \
get_set(d,'businessArea').intersection({'Engineering'}) and \
get_set(d,'Designation').intersection({'L1'}) and \
get_set(d,'Location').intersection({'NY'})
result = [d for d in test if validate(d)]
- 我得到了空列表
[{“id”:“3”,“name”:“C”,“businessArea”:[“engineering”],“Role”:[“Developer”],“designment”:[“L1”],“Location”:“NY”}]
添加一个以上的字典进行测试
[{'_index': '1',
'_type': '_doc',
'_id': '1',
'_score': 1.0,
'_source': {'id': '1',
'name': 'A',
'businessArea': [{'id': '25', 'name': 'Accounting'}],
'Role': ['Developer'],
'Designation': ['L2'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '2',
'_score': 1.0,
'_source': {'id': '2',
'name': 'B',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '3',
'_score': 1.0,
'_source': {'id': '3',
'name': 'C',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}}]
[{'id':'1','name':'Group1','BusinessArea':[{'id':'14','name':'Accounting'},{'id':'3','name':'Accounting'},'designment':[{'id':'16','name':'L1'},{'id':'20','name':'L2','id':'L2','name':'Group1','BusinessArea':['id':'id':'14','Research'},{'id':'3','name':'Accounting'}],'Role':[{'id':'5032','name':'Tester'},{'id':'5033','name':'Developer'}],'designment':[{'id':'16','name':'L1','name':'L1',{'id':'20','name':'L2'},{'id':'25','name':'L2'},},{'id':'1','name':'Group1','BusinessArea id':'14','Research'},{'id':'3','name':'Accounting'}],'Role':[{'id':'5032','name':'Developer'},{'id':'5033','name':'Developer'}],'designment':[{'id':'16','name':'L1'},{'id':'20','name':'L2'},{'id':'25','name':'L2'}]
要测试的第二本词典
[{'_index': '1',
'_type': '_doc',
'_id': '1',
'_score': 1.0,
'_source': {'id': '1',
'name': 'A',
'businessArea': [{'id': '25', 'name': 'Accounting'}],
'Role': ['Developer'],
'Designation': ['L2'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '2',
'_score': 1.0,
'_source': {'id': '2',
'name': 'B',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '3',
'_score': 1.0,
'_source': {'id': '3',
'name': 'C',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}}]
试着这样做:
myList = [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" },
{ "id": "2", "name": "B", "businessArea": [ "Engineerring" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
{ "id": "3", "name": "C", "businessArea": [ "Engineerring" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
for x in myList:
if (("Tester" in x['Role'] or "Developer" in x['Role']) and (x["Designation"] == "L1")) and (x["Location"] == "NY"):
print(x)
您的示例中有一个打字错误(
engineering
):
修复原始代码 您可以通过在函数validate中的if语句中替换或和来运行代码
def get_set(d,field):
return {d[field]} if isinstance(d[field], str) else set(d[field])
# we use this to filter
def validate(d):
# change or to and in if statement
if 'Role' in d and 'businessArea' in d and 'Designation' in d and 'Location' in d :
return get_set(d,'Role').intersection({'Developer','Tester'}) and \
get_set(d,'businessArea').intersection({'Engineering'}) and \
get_set(d,'Designation').intersection({'L1'}) and \
get_set(d,'Location').intersection({'NY'})
test = [ { "id": "1", "name": "A", "businessArea": [ "Accounting" ], "Designation": [ "L2" ], "Location":"NY" },
{ "id": "2", "name": "B", "businessArea": [ "Engineering" ], "Role": [ "Tester","Developer" ], "Designation": [ "L1" ],"Location":"CA" },
{ "id": "3", "name": "C", "businessArea": [ "Engineering" ], "Role": [ "Developer" ], "Designation": [ "L1" ],"Location":"NY" }]
result = [d for d in test if validate(d)]
print(result)
输出
[{'id': '3',
'name': 'C',
'businessArea': ['Engineering'],
'Role': ['Developer'],
'Designation': ['L1'],
'Location': 'NY'}]
处理嵌套字典的新算法
def contains(item, field, values):
'''
Search through nested dictionary starting a key field
to find if a value is in values
'''
if field:
item = item.get(field) # Get value of field
if item is None:
return None # Done if field not found
if isinstance(item, list):
return any(contains(v, None, values) for v in item) # recursively check if any value in list in values
if isinstance(item, dict):
return any(contains(v, None, values) for v in item.values()) # recursively check if any value in dictionary in values
return item in values # Not list or dict, so check if item in values
def validate(d):
return contains(d, 'Role', {'Developer','Tester'}) and \
contains(d, 'businessArea', {'Engineering'}) and \
contains(d, 'Designation', {'L1'}) and \
contains(d, 'Location', {'NY'})
测试
def contains(item, field, values):
'''
Search through nested dictionary starting a key field
to find if a value is in values
'''
if field:
item = item.get(field) # Get value of field
if item is None:
return None # Done if field not found
if isinstance(item, list):
return any(contains(v, None, values) for v in item) # recursively check if any value in list in values
if isinstance(item, dict):
return any(contains(v, None, values) for v in item.values()) # recursively check if any value in dictionary in values
return item in values # Not list or dict, so check if item in values
def validate(d):
return contains(d, 'Role', {'Developer','Tester'}) and \
contains(d, 'businessArea', {'Engineering'}) and \
contains(d, 'Designation', {'L1'}) and \
contains(d, 'Location', {'NY'})
测试1
产出1
测试2
产出2
测试3
产出3
测试4(有问题的第二本词典)
输出
您应该在发布时提供您的值和执行跟踪。预期的操作在哪里发生故障?是否与访问值有关?使用
if
语句,使您的函数默认返回None
?比较结果是否为空?我的代码有什么问题?我将等待您添加请重复我提出的调试要点。为了避免函数get_set中的键错误,请将函数validate中的所有或更改为和,您当前的代码将成功运行。@DarrylG,如果我给出和将正确抛出错误,我需要检查字典中是否有这些键。您可以看到first dictionary没有角色
,因此它会抛出key error这就是答案。您的代码没有问题。现在尝试运行它。您可以在第一个dictionary中看到没有角色
您必须考虑某个键不存在的情况。如果我的业务领域是数组列表,那么如何循环。[{'id':'1','name':'A','businessArea':[{'id':'25','name':'Accounting'}],'Role':['Developer'],'Assignment':['L2'],'Location':'NY'},{'id':'2','name':'B','businessArea':[{'id':'25','name':'Engineerring'}],'Role':['Tester','Developer'],'Assignment':['L1'],'Location':'NY'},{'id':'3','name':'C','businessArea':[{'id':'25','name':'engineering'}],'Role':['Tester','Developer'],'designment':['L1'],'Location':'NY'}]
@Nons在本例中,businessarea是一个包含一个字典的列表。这是最坏的情况,还是更复杂?与businessarea一样,role也是一个包含大量id的字典列表,我需要检查每个属性中的name
(如businessarea['name'],'role'['name'])我又增加了一本字典test@Nons--添加了一个可以处理任意嵌套字典的新算法。
[{'id': '3', 'name': 'C', 'businessArea': ['Engineering'], 'Role': ['Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
test = [{'id': '1', 'name': 'A', 'businessArea':[{'id': '25', 'name': 'Accounting'}], 'Role': ['Developer'], 'Designation': ['L2'], 'Location': 'NY'}, {'id': '2', 'name': 'B', 'businessArea':[{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea':[{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
result = [d for d in t if validate(d)]
print(result)
[{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
test = [{ 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting' }, { 'id': '3', 'name': 'Accounting' } ],'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }, ] }, { 'id': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research' }, { 'id': '3', 'name': 'Accounting' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }]
result = [d for d in t if validate(d)]
print(result)
[{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]
test = [{'_index': '1',
'_type': '_doc',
'_id': '1',
'_score': 1.0,
'_source': {'id': '1',
'name': 'A',
'businessArea': [{'id': '25', 'name': 'Accounting'}],
'Role': ['Developer'],
'Designation': ['L2'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '2',
'_score': 1.0,
'_source': {'id': '2',
'name': 'B',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}},
{'_index': '1',
'_type': '_doc',
'_id': '3',
'_score': 1.0,
'_source': {'id': '3',
'name': 'C',
'businessArea': [{'id': '25', 'name': 'Engineering'}],
'Role': ['Tester', 'Developer'],
'Designation': ['L1'],
'Location': 'NY'}}]
result = [d for d in t if validate(d)]
print(result)
[{'id': '2', 'name': 'B', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}, {'id': '3', 'name': 'C', 'businessArea': [{'id': '25', 'name': 'Engineering'}], 'Role': ['Tester', 'Developer'], 'Designation': ['L1'], 'Location': 'NY'}]