简体   繁体   English

从多列表字典中仅迭代一个字典列表

[英]Iterate only one dict list from multi list dict

I have a dict of lists like so: 我有这样一个列表的字典:

edu_options = { 'Completed Graduate School' : ['medical','litigation','specialist'...],

            'Completed College' : ['linguistic','lpn','liberal','chicano'... ],

          'Attended College' : ['general','inprogress','courseworktowards','continu'...],

My original code without an attempt at hierarchical matching: 我的原始代码没有尝试层次匹配:

for edu_level in edu_options: 
       for option in edu_options[edu_level]

                 if option in cleaned_string:
                        user =  edu_level
                        return user
                  else: continue

I'm comparing a string to these lists and returning the key. 我正在将字符串与这些列表进行比较并返回键。 I want to do it in a hierarchical way. 我想以分层方式进行。

 for edu_level in edu_options: 
        for option in edu_options[edu_level]:

            if cleaned_string in edu_options["Completed Graduate School"]: 
                  user = "Completed Graduate School"
                  return user                              

            elif cleaned_string in edu_options["Completed College"]:
                  user = "Completed College"
                  return user 

             elif option in cleaned_string:
                  user =  edu_level
                  return user

These if statements work for the majority of comparison str but don't pick up a few cases. 这些if语句可用于大多数比较str,但不会涉及少数情况。 For the first and second if statement, I only want to compare it to the respective list such as "Completed Graduate School". 对于第一和第二个if语句,我只想将其与相应的列表进行比较,例如“完成研究生院”。 Is there a way to iterate through only that list without using another for loop? 有没有办法只遍历该列表而不使用另一个for循环? Something like 就像是

Ex: string = Bachelor of Arts: Communication and Civil Service
    cleaned_string = bachelorofartscommunicationandcivilservice
    option = iterating through each item(str) of lists in edu_option 

I want the graduate and college lists to be run through first because they are smaller and more specific. 我希望首先列出毕业生和大学名单,因为它们较小且更具体。 The error I'm trying to correct is another larger list in edu_options contains sub str that matches to cleaned_string incorrectly. 我要纠正的错误是edu_options中的另一个较大列表,其中包含与stred_string错误匹配的sub str。

How about this: 这个怎么样:

for key, val_list in edu_options.items():
    if key == "Completed Graduate School":
        if cleaned_string in val_list:
            #do something
    #Similarly for remaining key types

This way, you are restricting the checks specifically to the key types. 这样,您将检查专门限制在密钥类型上。

 for edu_level in edu_options: 
        for option in edu_options[edu_level]:

            if cleaned_string in edu_options["Completed Graduate School"]: 
                  user = "Completed Graduate School"
                  return user                              

            elif cleaned_string in edu_options["Completed College"]:
                  user = "Completed College"
                  return user 

             elif option == cleaned_string:
                  user =  edu_level
                  return user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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