从python中defaultdict(list)中的键值中弹出一个键值

时间:2018-01-18 17:45:46

标签: python python-3.x python-2.7 tuples defaultdict

我有defaultdict(lambda: defaultdict(list))

jenkins_job_dict = defaultdict(<function <lambda> at 0x10428b230> , {
    'newswires': defaultdict(<type 'list'> , {
        'commodities': ['blue_virginia', 'green_virginia'],
        'nss_autopub_launch': ['blue_virginia'],
        'sample': ['blue_oregon', 'blue_virginia'],
        'commodities_efs': ['blue_virginia'],
        'gnews': ['blue_virginia']
    }),
    'usr': defaultdict(<type 'list'> , {
        'gatekeeper': ['blue_virginia'],
        'membership_services': ['green_virginia'],
        'membership_micro_db': ['green_virginia'],
        'pam_ml': ['blue_virginia', 'green_virginia'],
        'pam_rds': ['blue_virginia'],
        'preferences_api_db': ['blue_virginia', 'green_virginia'],
        'cs_microservice': ['blue_virginia'],
        'sample': ['blue_oregon', 'blue_virginia'],
        'preferences_api': ['blue_virginia'],
        'pam_app': ['blue_virginia', 'green_virginia']
    })
})

我需要从值中弹出第一个键值。因此,从两个键一次弹出一个(在键键内不需要订购):

'commodities': ['blue_virginia', 'green_virginia']
'gatekeeper': ['blue_virginia']
'nss_autopub_launch': ['blue_virginia'] 
'membership_services': ['green_virginia']
…

我有以下代码:

def create_jenkins_job_dict(all_jobs):
    jenkins_job_dict = defaultdict(lambda: defaultdict(list))
    for job in all_jobs:
        if job.startswith("djin_a"):
            stackname = job.split('_', 2).pop(2)
            lz = job.split('_', 2).pop(1)[1:]
            stack_info = parse_stack_info(stackname, lz)
            if stack_info is not None:
                for key, value in stack_info.items():
                    if (value == "up"):
                        jenkins_job_dict[lz][stackname].append(key)
    return jenkins_job_dict

all_jobs = ['cost_explorer', 'cucumber', 'cucumber_reporter', 'cucumber_stacks_cleanup', 'cucumber_stacks_single_account', 'cucumber_stacks_update', 'deptwo.0-workflow', 'dispatcher-policy-setup', 'djin_abuild_active_directory_iandp_domains', 'djin_abuild_artifactory', 'djin_abuild_barrage_slaves', 'djin_abuild_cf_test_1', 'djin_abuild_epam', 'djin_abuild_epportal', 'djin_abuild_gate', 'djin_abuild_groups', 'djin_abuild_hostname_dispatcher_2', 'djin_abuild_jenkins_slaves', 'djin_abuild_netapp_ontap', 'djin_abuild_papi', 'djin_abuild_pibsec_se', 'djin_abuild_sonar_slave', 'djin_abuild_sonarqube', 'djin_acntsvc_category_manager', 'djin_acntsvc_consumer_search_marklogic_db', 'djin_acntsvc_searchh'] # list of names of jobs in jenkins.
jenkins_job_dict = create_jenkins_job_dict(all_jobs) # basically produces the above dict.
while jenkins_job_dict:
    stack_list = jenkins_job_dict.pop(min(jenkins_job_dict,key=jenkins_job_dict.get))
    stack = stack_list.pop(min(stack_list, key=stack_list.get))
    print (stack)

这不能得到我所需要的,这是最诡计多端的做法吗?

1 个答案:

答案 0 :(得分:2)

这就是你要找的东西:

outer={'a':{'b':[1,2,3], 'c':[4,5,6]}, 'd':{'e':[7,8,9], 'f':[10,11,12]}}
while outer:
    outer_key, inner = outer.popitem()
    while inner:
        inner_key, inner_value = inner.popitem()