[英]How to access a key that is in a dictionary within another dictionary?
Here I have this code:我这里有这段代码:
import emoji
txt = str(input('String: '))
for emoji in emoji.EMOJI_DATA:
if emoji.EMOJI_DATA['en'] == txt:
print('The emoji would be here')
With that code, I get this error: Keyerror['en'] , because of course, there is no 'en'
within EMOJI_DATA, so, how could I access what would be the emoji key here?使用该代码,我得到了这个错误: Keyerror['en'] ,因为当然,EMOJI_DATA 中没有
'en'
,所以,我怎么能访问这里的表情符号键?
EMOJI_DATA = {
'🥇': {
'en' : ':1st_place_medal:',
'status' : emoji.STATUS["fully_qualified"],
'E' : 3,
'de': ':goldmedaille:',
'es': ':medalla_de_oro:',
'fr': ':médaille_d’or:',
'pt': ':medalha_de_ouro:',
'it': ':medaglia_d’oro:'
},
...
}
Your code should be like this as you can't access inner key directly.您的代码应该是这样的,因为您不能直接访问内部密钥。
import emoji
txt = str(input('String: '))
for emoji in emoji.EMOJI_DATA:
if emoji.EMOJI_DATA['🥇']['en'] == txt:
print('The emoji would be here')
First of all, you don't need to loop through all items in emoji.EMOJI_DATA
.首先,您不需要遍历
emoji.EMOJI_DATA
中的所有项目。 You may make use of lambda
which is more efficient.您可以使用效率更高的
lambda
。
import emoji
txt = ":1st_place_medal:"
emojiList = emoji.EMOJI_DATA
filteredList = list(filter(lambda x: x[1]["en"] == txt, emojiList.items()))
if(len(filteredList) > 0):
print("Emoji is ",filteredList[0][0])
print("Details are ",filteredList[0][1])
else:
print("Not Found")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.