# 昆虫名字到数字类别的映射关系
INSECT_NAMES = ['Boerner', 'Leconte', 'Linnaeus',
'acuminatus', 'armandi', 'coleoptera', 'linnaeus']
# 昆虫名字到数字类别的映射关系
def get_insect_names():
"""
return a dict, as following,
{'Boerner': 0,
'Leconte': 1,
'Linnaeus': 2,
'acuminatus': 3,
'armandi': 4,
'coleoptera': 5,
'linnaeus': 6
}
It can map the insect name into an integer label.
"""
insect_category2id = {
}
for i, item in enumerate(INSECT_NAMES):
print("i:{}".format(i))
print("item:{}".format(item))
insect_category2id[item] = i
return insect_category2id
a = get_insect_names()
Part of the code of Baidu Feijia above, here is a note, compare the dictionary that uses enumerate to perform digital mapping to create a lot, anyway, it is like this
Output
i:0
item:Boerner
i:1
item:Leconte
i:2
item:Linnaeus
i:3
item:acuminatus
i:4
item:armandi
i:5
item:coleoptera
i:6
item:linnaeus
>>> a
{
'Boerner': 0, 'Leconte': 1, 'Linnaeus': 2, 'acuminatus': 3, 'armandi': 4, 'coleoptera': 5, 'linnaeus': 6}
It can be seen from the output that enumerate will automatically generate a number of the corresponding size according to the position of the list element, thereby generating a dictionary of the corresponding number