我有一个像这样的列表:
char** arglist1, **arglist2;
我的原始代码没有尝试分层匹配:
char**arglist1, **arglist2;
我正在将字符串与这些列表进行比较并返回密钥。我想以分层的方式做到这一点。
edu_options = { 'Completed Graduate School' : ['medical','litigation','specialist'...],
'Completed College' : ['linguistic','lpn','liberal','chicano'... ],
'Attended College' : ['general','inprogress','courseworktowards','continu'...],
这些if语句适用于大多数比较str,但不适用于少数情况。对于第一个和第二个if语句,我只想将它与相应的列表(例如“已完成的研究生院”)进行比较。有没有办法只迭代该列表而不使用另一个for循环?像
这样的东西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
我希望毕业生和大学名单能够首先运行,因为它们更小,更具体。我正在尝试纠正的错误是edu_options中的另一个更大的列表包含与clean_string不正确匹配的子str。
答案 0 :(得分:1)
这个怎么样:
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
这样,您将检查专门限制为密钥类型。
答案 1 :(得分:0)
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