Python 3.x 尝试按字典列表分组时出错python 3.5
我正试图完成类似问题的事情。但是我得到了一个关键错误,因为并不是所有的列表都是平等创建的。。我不确定对我来说最好的行动方案是什么 我的dict列表是动态的,我正在尝试匹配'id'的键/值 一个例子是:Python 3.x 尝试按字典列表分组时出错python 3.5,python-3.x,dictionary,Python 3.x,Dictionary,我正试图完成类似问题的事情。但是我得到了一个关键错误,因为并不是所有的列表都是平等创建的。。我不确定对我来说最好的行动方案是什么 我的dict列表是动态的,我正在尝试匹配'id'的键/值 一个例子是: data_keys: [[{ 'pre_conditions': [], 'id': 436, 'tc_steps': [{ 'expected': 'Enter_APN: PleaseentertheAPN', 'content':
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条(在本例中)的dict
以下是我执行此特定任务的代码:
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)
上述代码中发现了两个问题:
itemgetter
中使用“tc\u步骤”
,而键是“tc\u步骤”
“tcs\u cannot\u route”
,而某些记录中不存在此密钥可能还有其他问题。那么错误是什么,需要的输出是什么?@vincentmajor由于字典中的键不匹配,这是一个键错误。我正试图找到一个解决方案,如果groupby不是答案的话,如何做到这一点。是的,我修复了tc_步骤——我理解这个问题是因为我在所有字典中没有相同的键。我正在想办法解决这个问题?groupby可能不是解决方案……在这种情况下,我建议您发布一个新问题,显示您的数据以及处理数据的预期输出。在这里,你关注的是“如何”而不是“你想要实现什么”,这似乎会导致一条死胡同。