如何在python字典中为每个键添加多个值 [英] How to add multiple values per key in python dictionary

查看:159
本文介绍了如何在python字典中为每个键添加多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要输出一个名称列表,其中每个名称对应三个数字,但是我不知道如何编码,这是我可以像cat1 = {"james":6, "bob":3}这样的字典来实现的方法,但是具有三个值每个键?

My program needs to output a list of names with three numbers corresponding to each name however I don't know how to code this is there a way I could do it as a dictionary such as cat1 = {"james":6, "bob":3} but with three values for each key?

推荐答案

每个键的值可以是一个集合(无序元素的不同列表)

The value for each key can either be a set (distinct list of unordered elements)

cat1 = {"james":{1,2,3}, "bob":{3,4,5}}
for x in cat1['james']:
    print x

或列表(元素的有序序列)

or a list (ordered sequence of elements )

cat1 = {"james":[1,2,3], "bob":[3,4,5]}
for x in cat1['james']:
    print x

这篇关于如何在python字典中为每个键添加多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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