python-3.x 拆分列表中的单词并迭代字典

ylamdve6  于 2023-03-13  发布在  Python
关注(0)|答案(4)|浏览(141)

我有一些代码,我在文本中接收一串语言。
我的目标是将这个输入转换成一个列表,并在字典中迭代这个列表,以用作值输出的键。
我期望的输出为[57、20、22、52、60...等],但目前我正在接收
[57、无、无、无、无、无、无......等]
我的第一个输出是正确的,但之后,它似乎没有找到正确的值在dict.
代码如下。

l_languages = []

language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}

data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"

language_list = data.split(',')

for language in language_list:
    id = language_dict.get(language)
    l_languages.append(id)
    
print(l_languages)

current output = [57, None, None, None, None, None, None....etc]
hm2xizp9

hm2xizp91#

您忽略了语言列表中的白色。2您应该删除开头和结尾的空白,然后访问您的dict。
如果你只是在''处拆分列表,那么在后面的每种语言前面都有一个白色。2只是第一种语言没有,这解释了你当前的输出

0yycz8jy

0yycz8jy2#

看看你的language_list,它前面有空格,你需要在每个元素上调用strip(),然后得到你期望的结果

l_languages = []

language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}

data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"

language_list = data.split(',')
print(language_list)
for language in language_list:
    val = language_dict.get(language.strip())
    l_languages.append(val)
    
print(l_languages)

['Afrikaans', ' Arabic', ' Assistive communication', ' AUSLAN', ' Bosnian', ' Burmese', ' Cantonese', ' Croation', ' Dutch'] # list with leading spaces
[57, 20, 21, 22, 52, 60, 23, 54, 50] # right result
wqsoz72f

wqsoz72f3#

l_languages = []
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21, 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23, 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28, 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"
language_list=[x.strip() for x in data.split(',')]
for language in language_list:
    id = language_dict.get(language)
    l_languages.append(id)
#output
[57, 20, 21, 22, 52, 60, 23, 54, 50]
yks3o0rb

yks3o0rb4#

最简单的方法

#Devil
language_dict = { 'Afrikaans' : 57, 'Arabic' : 20, 'Assistive communication' : 21,
                 'AUSLAN' : 22, 'Bosnian' : 52,'Burmese' : 60, 'Cantonese' : 23,
                 'Croation' : 54, 'Dutch' : 50,'French' : 24, 'German' : 25, 'Greek' : 26,
                 'Hindi' : 27, 'Indigenous Australian' : 310, 'Indonesian' : 56, 'Italian' : 28,
                 'Japanese' : 62, 'Korean' : 48, 'Mandarin' : 29, 'Nepali' : 55, 'Polish' : 30}
data = "Afrikaans, Arabic, Assistive communication, AUSLAN, Bosnian, Burmese, Cantonese, Croation, Dutch"

data_list = data.split(",") #split the data
data_list = [d.strip() for d in data_list] #remove white space
l_languages = [language_dict[z] for z in data_list] #find the value using key
print(data_list)
print(l_languages)

相关问题