I've got some code where I receive a string of languages in a text.
My goal is to turn this input into a list and iterate through this list in a dictionary to use as a key for value outputs. I send this output to a list to use later.
The output I am expecting is [57, 20, 22, 52, 60... etc] but currently, I am receiving
[57, None, None, None, None, None, None....etc]
My first output is correct but after that, It doesn't seem to find the correct value in the dict.
Code below.
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]
CodePudding user response:
you are neglecting the white space in your language list. You should remove the leading and trailing white space and the access your dict.
if you just split the list at ',' then there is a leading white space in front of every following language. Just not on the first one, which explains your current output
CodePudding user response:
Look at your language_list
. It has leading whitespace. You need to call strip()
on each element and you get your expected result
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
CodePudding user response:
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]