Home > Mobile >  Iterating a list of strings over a dictionary (keys and values) in Python
Iterating a list of strings over a dictionary (keys and values) in Python

Time:10-31

Here we go again. One 'fairly easy' exercise that has finally given me headache.

Make a program that uses a lookup table to convert any set of alphabets into their corresponding NATO phonetic alphabets (https://en.wikipedia.org/wiki/NATO_phonetic_alphabet). Also implement the inverse function.

  • Input: cat
  • Output: charlie alfa tango

Inverse function:

  • Input : charlie alfa tango
  • Output : cat

I have defined a dictionary with the alphabet and its Nato equivalent for each letter. Then, I have defined a function that will take elements from a list as input. Letter corresponds to my keys in the dictionary and letter conversion corresponds to my values. The function works in the following cases:

  • When I enter a single character, it will give me its 'Nato' equivalent. (comparing the string with letter and outputting the letter conversion)
  • When I enter a string that is Nato alphabet it will output the letter. (comparing the string with the letter conversion and outputting the letter)

I'm getting stuck when I need to evaluate the lenght of the string from my list, and then convert each character into its Nato alphabet.

nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}


#Passing through a list 

def convert(word_to_convert):
    word_converted= []
    for i in word_to_convert:
        for letter, letter_conversion in nato_alphabet.items():
            if i == letter:
                word_converted.append(letter_conversion)

            if i == letter_conversion:  
                word_converted.append(letter)   

            if len(i) == letter:
                word_converted.append(letter_conversion )                   
    return  word_converted                  
    

print(convert(['c', 'Alfa', 'moon']))   

Thank you everyone for your help !

Diana

CodePudding user response:

Use:

def convert(word_to_convert):
    word_converted = []
    for i in word_to_convert:
        word_converted.append(" ".join(nato_alphabet[ii.lower()] for ii in i))
    return word_converted


print(convert(['c', 'Alfa', 'moon']))

Output

['Charlie', 'Alfa Lima Foxtrot Alfa', 'Mike Oscar Oscar November']

As an alternative:

# Passing through a list
def convert(words_to_convert):
    word_converted = []
    # iterate over each word
    for word in words_to_convert:
        nato_encoded_list = []
        # iterate over each character in word
        for char in word:
            # fetch the corresponding value from nato_alphabet
            nato_encoded_list.append(nato_alphabet[char.lower()])
        
        # join the encoded words and add to word_converted list         
        word_converted.append(" ".join(nato_encoded_list))
    return word_converted

Basically iterate over each, word and for each character in the word fetch the nato_alphabet value.

CodePudding user response:

This might let me know if something is unclear

nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}
nato_alphabet_inverse = {v.lower():k.lower() for k,v in nato_alphabet.items()}
nato_alphabet = {k.lower():v.lower() for k,v in nato_alphabet.items()}
# print(nato_alphabet_inverse)

def convert(list_to_convert):
    return_list = []
    for each_item in list_to_convert:
        # print(each_item)
        if " " in each_item:  # Sentence thus must be converted to Alphabets
            l_ = each_item.split()
            temp_list = []
            for each_word in l_:
                # print(each_word)
                temp_list.append(nato_alphabet_inverse[each_word.lower()])
                # return_list.append()
            return_list.append("".join(temp_list))


        else:  # Convert to equivalent Nato
            temp_list = []
            for chr in each_item:
                # print(chr)
                temp_list.append(nato_alphabet[chr.lower()])
            str_ = " ".join(temp_list)
            return_list.append(str_)
    return return_list
print(convert(['cAT',"charlie alfa tango"]))
  • Related