简体   繁体   中英

pop a key, value from values of a key in defaultdict(list) in python

I have a 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']
    })
})

I need to pop the first key,value from the values. So pop one at a time from both keys (within the key key doesnt have to be ordered) :

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

I have the following code :

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)

This doesnt get what I need , whats the most pythonic way of doing this ?

Is this what you are looking for:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM