【问题标题】:Python updating dictionary but need to get the key pair?Python 更新字典但需要获取密钥对?
【发布时间】:2017-04-22 16:37:10
【问题描述】:

鉴于此字典格式:

名称:(id,type1,type2,hp,攻击,防御,速度,世代,传奇)
dict={'Bulbasaur': (1, '草', '毒药', 45, 49, 49, 45, 1, False)}

我需要浏览数据库(多个口袋妖怪的字典,其统计数据采用提供的格式)并找到哪些口袋妖怪具有传奇地位,这是一个布尔值。我需要数出传说中的类型并将它们放入新字典中。

例如,如果 Bulbasaur 是传奇人物,草类型=1 毒类型=1。新的字典项是:

new_dict={“草”: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_types 函数的作用:

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)

【问题讨论】:

    标签: python-3.x dictionary key


    【解决方案1】:

    假设您只想计算一个类型在传奇口袋妖怪中出现的次数,而不使用像 pandas 之类的花哨的东西(您可能应该对您的数据收集进行处理,或者可能是一个小 sql db)

    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.
    

    【讨论】:

    • 不幸的是,我不允许导入任何模块。 @nimish
    • 已编辑,Counter 只是一个带有一些增强功能的字典。
    • 当没有列为类型时,它会遇到错误。 {'Dragon': 1, 'Fire': 4, None: 2, 'Flying':[37 chars]': 1} != {'Grass': 1, 'Fire': 4, 'Flying': 2, 'Poiso[41 chars]': 1}如何过滤掉'None'类型?
    • 嗯,你在循环体中有两种类型——你应该能够过滤掉只有一种类型的情况。这是家庭作业吗?
    • 我有一个类似的不同任务,但我想看看我是否可以使用 pokemon stats 来完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2023-03-19
    • 1970-01-01
    • 2021-09-25
    • 2016-02-17
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多