[英]Iterating a list of strings over a dictionary (keys and values) in Python
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 ).制作一个程序,使用查找表将任何一组字母转换为其相应的北约语音字母 ( https://en.wikipedia.org/wiki/NATO_phonetic_alphabet )。 Also implement the inverse function.还实现了反函数。
Inverse function:反函数:
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:该功能适用于以下情况:
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戴安娜
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.基本上遍历每个单词并为单词中的每个字符获取nato_alphabet
值。
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"]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.