Python 3.x Python正在更新字典,但需要获取密钥对吗?

Python 3.x Python正在更新字典,但需要获取密钥对吗?,python-3.x,dictionary,key,Python 3.x,Dictionary,Key,给定此字典格式: 姓名:(id、类型1、类型2、生命、攻击、防御、速度、世代、传奇) dict={'Bulbasaur':(1,'Grass','Poison',45,49,49,45,1,False)} 我需要浏览数据库(多个口袋妖怪的字典,并以提供的格式显示它们的统计数据),然后找到哪个口袋妖怪具有传奇状态,这是一个布尔值。我需要数一数传奇人物的类型,然后把它们放到一本新字典里 例如,如果Bulbasaur是传说中的,草型=1毒型=1。新字典项将是: 新词={“草”:1,“毒”:1} 我编写

给定此字典格式:

姓名:(id、类型1、类型2、生命、攻击、防御、速度、世代、传奇)
dict={'Bulbasaur':(1,'Grass','Poison',45,49,49,45,1,False)}

我需要浏览数据库(多个口袋妖怪的字典,并以提供的格式显示它们的统计数据),然后找到哪个口袋妖怪具有传奇状态,这是一个布尔值。我需要数一数传奇人物的类型,然后把它们放到一本新字典里

例如,如果Bulbasaur是传说中的,草型=1毒型=1。新字典项将是:

新词={“草”:1,“毒”:1}

我编写了代码来提取类型,然后计算哪些类型是传说中的,但我一直在研究如何获得带有类型和计数编号的最终字典

以下是我的代码:

def legendary_count_of_types(db):

    Fire=0
    Grass=0
    Flying=0
    Poison=0
    Dragon=0
    Water=0
    Fighting=0
    Ground=0
    Ghost=0
    Rock=0
    Ice=0
    d={}
    for key,values in db.items():
        status=values[8]
        if status==True:
            type_list=get_types(db)
            for item in type_list:
                if item=='Fire':
                    Fire+=1
                if item=='Grass':
                    Grass+=1
                if item=='Flying':
                    Flying+=1
                if item=='Poison':
                    Poison+=1
                if item=='Dragon':
                    Dragon+=1
                if item=='Water':
                    Water+=1
                if item=='Fighting':
                    Fighting+=1
                if item=='Ground':
                    Ground+=1
                if item=='Ghost':
                    Ghost+=1
                if item=='Rock':
                    Rock+=1
                if item=='Ice':
                    Ice+=1
    d.update()
    #how do I get the key value pair?
    return d
下面是我的get_type函数的作用:

def get_types(db):
    l=[]
    s=[]
    for key,values in db.items():
        types1=str(values[1])
        types2-str(values[2])
        l.apppend(types1)
        l.append(types2)
    for i in l:
        if i not in s:
            s.append(i)
            if 'None' in s:
                s.remove('None')
    final_list=s
    return sorted(final_list)

假设您只想知道某个类型在传奇口袋妖怪中出现的次数,而不需要使用任何像熊猫这样的花哨东西(您可能应该使用数据收集或一点sql db来完成这项工作)


不幸的是,我不允许导入任何模块@nimishEdited,Counter只是一个有一些增强功能的字典。当没有任何类型被列为类型时,它会出错。{'Dragon':1,'Fire':4,None:2,'Flying':[37个字符]:1}={'Grass':1,'Fire':4,'Flying':2,'Poiso[41个字符]':1}如何筛选出'None'类型?好的,在循环体中有两种类型--应该能够筛选出只有一种类型的情况。这是一个家庭作业吗?我有一个类似的不同作业,但我想看看我是否可以用口袋妖怪统计数据来完成。
type_counter = dict() # or use collections.Counter
for name, attributes in db.items()
    is_legendary = attributes[8]
    if is_legendary:
        type1 = attributes[1]
        type2 = attributes[2]  
        type_counter[type1] = type_counter.get(type1, 0) + 1
        type_counter[type2] = type_counter.get(type2, 0) + 1

# type_counter will now be a dictionary with the counts.