如何将列表存储为字典-Python [英] How to store a list as a dictionary - python

查看:99
本文介绍了如何将列表存储为字典-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何存储在列表中,但是如何将这些值存储为字典?

I know how to store in a list but how do I store these values as a dictionary instead?

    items = []
    for a in range(10):
        items.append([])
        for b in range(10):
            if function() == condition:
                items[a].append(WRONG)
    for a,b in oldItems:
        items[a][b] = RIGHT

到目前为止,我得出的结论是我可以将项目存储为 items = {} ,但是我不知道如何使用复制 .append 方法字典.我也不知道如何像 items [a] [b] 这样访问字典.有什么建议么?

So far I have come to the conclusion that I could store items as items = {} but I don't know how to duplicate the .append method with a dictionary. Nor do I know how to access a dictionary like items[a][b] does. Any suggestions?

推荐答案

声明字典:

dictionary = {}

添加到词典中

dictionary[newkey] = newvalue

访问字典:

print (dictionary[newkey])

返回:

newvalue

这篇关于如何将列表存储为字典-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆