字符串与字典中的列表相比

string compared to list in a dict

我将输入一个大的 strs 数据集来与带有列表的 dict 进行比较。例如,str 'phd' 将与这个 dict

中的 strs 进行比较
   edu_options = {'Completed College' : [ 'bachelor', 'ba', 'be', 'bs'....],
                  'Grad School' : ['phd','doctor'...] }

输入字符串来自edu_dict

edu_dict = {
        "A.S":"Attended Vocational/Technical",
        "AS":"Attended Vocational/Technical",
        "AS,":"Attended Vocational/Technical",
        "ASS,":"Attended Vocational/Technical",
        "Associate":"Attended Vocational/Technical",
        "Associate of Arts (A.A.),":"Attended Vocational/Technical",
        "Associate of Arts and Sciences (AAS)":"Attended Vocational/Technical",
        "B-Arch":"Completed College",
        "B-Tech":"Attended Vocational/Technical",
        "B.A. B.S":"Completed College",
        "B.A.,":"Completed College",
        "B.Arch,":"Completed College",
        "B.S":"Completed College",
        "B.S.":"Completed College",
        "B.S. in Management":"Completed College",
        "B.S.,":"Completed College",
        "BA":"Completed College",... 
    *The list is 169 items similar to this*
    }

clean_edu() 从 edu_dict 中获取密钥,删除标点符号、空格等。例如 'P.H.D.' 变为 'phd'。如果 'phd' 与这些列表中的任何一个 str 匹配,它应该 return 正确的键,在本例中为 'Completed Graduate'。对于我输入的大部分内容,正确的值已 returned。

def clean_edu(edu_entry):

    lower_case_key = edu_entry.lower() # changing the key to lower case

    chars_in = "-.,')("           #setting the chars to be translated
    chars_out = "      " 
    char_change = "".maketrans(chars_in, chars_out)        # replacing punctuation(char_in) with empty space(char_out)   

    clean = lower_case_key.translate(char_change)      #executing char_change 


    cleaned_string = re.sub(r'\s\s{0,}','',clean).strip()


    return cleaned_string


while user == "":
    for edu_level in edu_options: 
        for option in edu_options[edu_level]:
            if option in cleaned_string: 
                user = edu_level
                return user 

    user = "No match"

问题是 'bs' 对某些输入正确触发,但对其他输入则不正确。当我打印不匹配的 str 和它们的比较时

print ("Not Detected. Adding to txt" + '\t' + edu_entry + '\t' + cleaned_string + '\t' + option) 

Output: " Not Detected. Adding to txt     business        nursing 

其中 bs 是输入,l 是比较 str。在 edu_options dict 中没有值 'l' 所以我不明白这是从哪里来的。对于 'bs biology' 或 'bs business'.

等输入字符串,不会出现此问题

成功运行:

输入海峡:'P.H.D' 输出:'Completed Graduate School'

我不确定我是否理解你应该return当你在列表中找到匹配时,也许是那个列表的键?

在那种情况下,这应该有效:

>>> edu_options = {'Completed College' : [ 'bachelor', 'ba', 'be', 'bs'], 'Grad Shool': ['phd', 'doctor']}
>>> cleaned_string = 'phd'
>>> for key, value in edu_options.items():
...     if cleaned_string in value:         # value is the list
...         print key                       # inside a function, use return
...
>>> Grad Shool

编辑:我认为错误出在你的第二个循环中,看看会发生什么:

>>> edu_options = {'Completed College' : [ 'bachelor', 'ba', 'be', 'bs'], 'Grad Shool': ['phd', 'doctor']}
>>> for edu_level in edu_options: 
...     for option in edu_level:   # Right here
...         print option
... 
C
o
m
p
l
e
t
e
d

C
o
l
l
e
g
e
G
r
a
d

S
h
o
o
l
>>>

从那里 'l' 出来。