我试图完成与this问题相似的事情。但是我得到了关键错误,因为并非所有列表都是平等创建的。我不确定对我的案例最好的做法是什么。
我的词典列表是动态的,我试图匹配' id'的键/值。
示例列表是:
data_keys: [[{
'pre_conditions': [],
'id': 436,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press*'
},
{
'expected': 'Enter_ANI: ',
'content': ''
}],
'title': "Route from 'Start' to 'GetCallerDetail'"
}],
[{
'pre_conditions': [],
'id': 565,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
}],
'title': "Route from 'Greeting' to 'apn_match'"
}],
[{
'pre_conditions': [],
'id': 571,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': ''
}],
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension'"
}],
[{
'pre_conditions': [],
'id': 571,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': ''
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Again;ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': 'wait'
}],
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input & Recover"
}],
[{
'pre_conditions': [],
'id': 571,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': ''
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtensionNM1: Again;ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': 'press8'
}],
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Match & Recover"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input 3 Times Fail"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Match 3 Times Fail"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', No Input or No Match 3 Times Fail"
}],
[{
'pre_conditions': [],
'id': 571,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': ''
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: ',
'content': 'Novalidinputs'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtensionCNI1: ',
'content': 'wait'
}],
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input & Recover"
}],
[{
'pre_conditions': [],
'id': 571,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: Ifthefirst5lettersofthestreetnameforyourserviceaddressare[spellfirst5lettersofRAD.ServiceAddressStreetNamee.g.M-A-P-L-E]?press1.Otherwise,
press2.',
'content': ''
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtension: ',
'content': 'Novalidinputs'
},
{
'expected': 'ConfirmStreetNameIDV~NodeExtensionCNM1: ',
'content': 'press1'
}],
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Match & Recover"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input 3 Times Fail"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Match 3 Times Fail"
}],
[{
'tcs_cannot_route': 'Thisedgecannotberouted,
OnFailGoToempty',
'id': 571,
'title': "Route from 'apn_match' to 'ConfirmStreetNameIDV~NodeExtension', Confirm No Input or No Match 3 Times Fail"
}],
[{
'pre_conditions': [],
'id': [567],
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press*'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'no_apn_match: ',
'content': ''
},
{
'expected': 'AskDOB: ',
'content': ''
}],
'title': "Route from 'no_apn_match' to 'AskDOB'"
}],
[{
'pre_conditions': [],
'id': 566,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press*'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'no_apn_match: ',
'content': ''
}],
'title': "Route from 'Greeting' to 'no_apn_match'"
}],
[{
'pre_conditions': [],
'id': 564,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press*'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
}],
'title': "Route from 'GetCallerDetail' to 'Greeting'"
}],
[{
'pre_conditions': [],
'id': 568,
'tc_steps': [{
'expected': 'Enter_APN: PleaseentertheAPN',
'content': 'APN: 8552929487,
HollyBrowser: ,
'
},
{
'expected': 'Enter_Test_Date: Pleaseenterthedate',
'content': 'press4025554244'
},
{
'expected': 'Enter_ANI: ',
'content': ''
},
{
'expected': 'Greeting: ',
'content': 'press*'
},
{
'expected': 'apn_match: ',
'content': 'press4025554244'
},
{
'expected': 'AskDOB: ',
'content': ''
}],
'title': "Route from 'apn_match' to 'AskDOB'"
}]]
我想在这个列表中最终只有7个(在这种情况下)dicts
这是我执行此特定任务的代码:
from itertools import groupby
from operator import itemgetter
groups = groupby(data_keys, itemgetter('id', 'title', 'pre_conditions', 'tc_steps', 'tcs_cannot_route'))
edge_data = [{'id': groupinfo[0], 'title': groupinfo[1], 'pre_conditions': groupinfo[2], 'tc_steps': groupinfo[3], 'tcs_cannot_route': groupinfo[4]} for groupinfo, items in groups]
print(edge_data)
答案 0 :(得分:1)
上述代码中发现了两个问题:
"tc_step"
使用itemgetter
,而密钥为"tc_steps"
"tcs_cannot_route"
,而某些记录中不存在此密钥也可能存在其他问题。