将值附加到Python中的字典 [英] Appending values to dictionary in Python

查看:137
本文介绍了将值附加到Python中的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本我要附加到每个药物的字典,一个数字列表。像这样:

  append(0),append(1234),append(123)等

def make_drug_dictionary(data):
drug_dictionary = {'MORPHINE':[],
'OXYCODONE':[],
'OXYMORPHONE':[],
'METHADONE ':[],
'BUPRENORPHINE':[],
'HYDROMORPHONE':[],
'CODEINE':[],
'HYDROCODONE':[]}
prev =无
数据行:
如果prev为None或prev == row [11]:
drug_dictionary.append [row [11] []
返回drug_dictionary

我以后想要能够访问entirr集中的条目,例如'MORPHINE'


  1. 如何在drug_dictionary中附加一个数字? li>
  2. 如何稍后遍历每个条目?


解决方案

只需使用append:

  list1 = [1,2,3,4,5] 
list2 = [123,234,456]
d = {'a':[],'b':[]}
d ['a']。append(list1)
d ['a']。append(list2)
print d ['a']


I have a dictionary to which I want to append to each drug, a list of numbers. Like this:

append(0), append(1234), append(123), etc.

def make_drug_dictionary(data):
    drug_dictionary={'MORPHINE':[],
                     'OXYCODONE':[],
                     'OXYMORPHONE':[],
                     'METHADONE':[],
                     'BUPRENORPHINE':[],
                     'HYDROMORPHONE':[],
                     'CODEINE':[],
                     'HYDROCODONE':[]}
    prev = None
    for row in data:
        if prev is None or prev==row[11]:
            drug_dictionary.append[row[11][]
    return drug_dictionary

I later want to be able to access the entirr set of entries in, for example, 'MORPHINE'.

  1. How do I append a number into the drug_dictionary?
  2. How do I later traverse through each entry?

解决方案

Just use append:

list1 = [1, 2, 3, 4, 5]
list2 = [123, 234, 456]
d = {'a': [], 'b': []}
d['a'].append(list1)
d['a'].append(list2)
print d['a']

这篇关于将值附加到Python中的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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