在循环内将项目添加到字典 [英] Adding item to Dictionary within loop

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

问题描述

下面的数据是从网页中获取的,并且包含以下条目(如具有多行的表):

entry1: key1: value1-1, key2: value2-1, key3: value3-1
entry2: key1: value1-2, key2: value2-2, key3: value3-2
entry3: key1: value3-1, key2: value2-3, key3: value3-3
......
entry100: key1: value100-1, key2: value100-2, key3: value100-3

如何使用字典来存储此数据?数据来自列表,因此字典追加"应在循环中完成...

这是我当前的解决方案:

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list.update(case)

但是最后的case_list仅包含最后一个案例条目...有人可以帮我吗? 我希望case_list包含100个条目,而条目之间没有任何覆盖,之后我需要将其存储到DB.

解决方案

在您当前的代码中,Dictionary.update()所做的是它会更新(update表示值将从传入字典中传递的同一键的值覆盖).当前字典中的键,并将字典中的值作为参数传递给它(添加任何新的key:value对(如果存在)).单个平面词典不能满足您的要求,您需要词典列表或包含嵌套词典的词典.

如果您想要字典列表(列表中的每个元素都是条目的字典),则可以将case_list作为列表,然后将case附加到列表中(而不是update). /p>

示例-

case_list = []
for entry in entries_list:
    case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
    case_list.append(case)


或者您也可以拥有一个字典字典,字典中每个元素的键为entry1entry2等,值是该条目的相应字典.

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list[entryname] = case  #you will need to come up with the logic to get the entryname.

Below data is grasped from webpage and containing entries as below(like a table with many rows):

entry1: key1: value1-1, key2: value2-1, key3: value3-1
entry2: key1: value1-2, key2: value2-2, key3: value3-2
entry3: key1: value3-1, key2: value2-3, key3: value3-3
......
entry100: key1: value100-1, key2: value100-2, key3: value100-3

how can I use a dictionary to store this data? the data is from a list thus the 'dictionary append' should be done within a loop...

here is my current solution:

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list.update(case)

but the case_list in the end only contains the last case entry... Can somebody please help me on this? I would expect the case_list containing 100 entries w/o any overwriting among entries, and I will need to store it to DB afterwards.

解决方案

In your current code, what Dictionary.update() does is that it updates (update means the value is overwritten from the value for same key in passed in dictionary) the keys in current dictionary with the values from the dictionary passed in as the parameter to it (adding any new key:value pairs if existing) . A single flat dictionary does not satisfy your requirement , you either need a list of dictionaries or a dictionary with nested dictionaries.

If you want a list of dictionaries (where each element in the list would be a diciotnary of a entry) then you can make case_list as a list and then append case to it (instead of update) .

Example -

case_list = []
for entry in entries_list:
    case = {'key1': entry[0], 'key2': entry[1], 'key3':entry[2] }
    case_list.append(case)


Or you can also have a dictionary of dictionaries with the key of each element in the dictionary being entry1 or entry2 , etc and the value being the corresponding dictionary for that entry.

case_list = {}
for entry in entries_list:
    case = {'key1': value, 'key2': value, 'key3':value }
    case_list[entryname] = case  #you will need to come up with the logic to get the entryname.

这篇关于在循环内将项目添加到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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