Test/Python(20220101~)

Days26. 입력하는 영단어를 NATO Phonetic Alphabet 으로 변환

kiostory 2022. 3. 26. 17:37

 

* NATO Ponetic Alphabet이 정의된 csv를 이용함.

nato_phonetic_alphabet.csv
0.00MB

 

 

 

#TODO 1. using csv file, Create a dictionary in this format like:
#{"A": "Alfa", "B": "Bravo"}
data = pandas.read_csv("./nato_phonetic_alphabet.csv")
data_dict_comprehension = { row.letter:row.code
    for (index,row) in data.iterrows()
    }
#print(data_dict_comprehension)
#TODO 2. Create a list of the phonetic code words from a word that the user inputs.
word = input("Enter a word: ").upper()
phonetic_word_lists = []
for i in word:
    #phonetic_word_lists.append(code for (letter,code) in data_dict_comprehension.items() if i.upper() == letter)
    for (letter,code) in data_dict_comprehension.items():
        if i == letter:
            phonetic_word_lists.append(code)
print(phonetic_word_lists)

Enter a word: kioeom
['Kilo', 'India', 'Oscar', 'Echo', 'Oscar', 'Mike']

 

 

* TODO 2를 다시 간단하게.

  > 리스트를 별도로 지정하는 것도 필요없고

  > for문을 중복으로 돌리는 것도 필요없고

  > word의 알파벳과 비교도 간단히

#TODO 2. Create a list of the phonetic code words from a word that the user inputs.
word = input("Enter a word: ").upper()
phonetic_word_lists = [ data_dict_comprehension[letter] for letter in word ]
print(phonetic_word_lists)

Enter a word: korea
['Kilo', 'Oscar', 'Romeo', 'Echo', 'Alfa']