字符串与di中的列表进行比较

2024-05-29 04:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我将输入一个大的str数据集,与带有列表的dict进行比较。例如,str“phd”将与本词典中的str进行比较

   edu_options = {'Completed College' : [ 'bachelor', 'ba', 'be', 'bs'....],
                  'Grad School' : ['phd','doctor'...] }

输入str来自edu\u 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\u edu()从edu dict中取出密钥,删除标点、空格等。例如,“p.H.D.”变成“phd”。如果“phd”与这些列表中的任何一个str匹配,它应该返回正确的键,在本例中为“Completed Graduate”。对于我输入的大多数输入,都返回了正确的值。你知道吗

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”,所以我不明白这是从哪里来的。“生物学士”或“商业学士”等输入str没有出现此问题。你知道吗

成功运行:

输入str:'p.H.D'输出:'Completed Graduate School'


Tags: toincleandictoptionseduphdchar
1条回答
网友
1楼 · 发布于 2024-05-29 04:27:21

我不确定我是否理解当你在列表中找到匹配项时应该返回什么,也许是该列表的键?你知道吗

在这种情况下,这应该起作用:

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

从那里我出来了。你知道吗

相关问题 更多 >

    热门问题