Python, Как напечатать алфавиты для фонетики - PullRequest
0 голосов
/ 25 сентября 2019

Так что мне нужно создать программу, которая хранит фонетический алфавит в list/array.Программа запрашивает ввод у пользователя и превращает каждую введенную букву в фонетическую версию каждого символа.

e.g Apple = Alpha, Papa, Papa, Lima, Echo.

Я понятия не имею, как этого добиться, все, что мне удалось придумать, это

 Phonetics = ["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliet","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whiskey","X-Ray","Yankee","Zulu"]
Alphabetics = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
word = (input("Please enter your word: "))
for x in word:
print (x)

Ответы [ 3 ]

1 голос
/ 25 сентября 2019

Более Pythonic подход к этому

phonetics = {
    'a': 'Alpha',
    'b': 'Beta',
    'c': 'Charlie',
    ...
}
word = input("String: ")
for i in word:
    if i.lower() in phonetics.keys():
        print(phonetics[i.lower()])
    else:
        print(i)

Подробнее о словаре Python здесь .

0 голосов
/ 25 сентября 2019

Наиболее прямолинейно при использовании пониманий,

>>> Phonetics = ["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliet","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whiskey","X-Ray","Yankee","Zulu"]
>>> inp = input("Please enter your word:")
Please enter your word:alpha
>>> ",".join([j for i in inp for j in Phonetics if j.lower().startswith(i.lower())])'Alpha,Lima,Papa,Hotel,Alpha'
>>> 

Ответ:

Alpha,Lima,Papa,Hotel,Alpha

Примечание: для алфавитов вы можете использовать строка модуля вместо

import string
Alphabets = list(string.ascii_lowercase)

Второй подход,

генерирование алфавитов с использованием фонетики

>>> Phonetics = ["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliet","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whiskey","X-Ray","Yankee","Zulu"]
>>> Alphabets = {i[0].lower(): i for i in Phonetics}
>>> inp = raw_input("Please enter your word:")
Please enter your word:apple
>>> ",".join((Alphabets[i.lower()] for i in inp))
'Alpha,Papa,Papa,Lima,Echo'
>>> 
0 голосов
/ 25 сентября 2019

Это один из подходов.Создание словаря с алфавитами в качестве ключа и фонетики в качестве значения.

Пример:

Phonetics = ["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot","Golf","Hotel","India","Juliet","Kilo","Lima","Mike","November","Oscar","Papa","Quebec","Romeo","Sierra","Tango","Uniform","Victor","Whiskey","X-Ray","Yankee","Zulu"]
Alphabetics = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
check_value = dict(zip(Alphabetics, Phonetics)) # {'a': 'Alpha', 'b': 'Bravo', 'c': 'Charlie'......

word = (input("Please enter your word: "))

print(",".join(check_value[l.lower()] for l in word))
# --> Alpha,Papa,Papa,Lima,Echo
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...