简体   繁体   English

从另一个字典列表创建字典列表

[英]Creating a list of dictionaries from another list of dictionaries

Sorry for the long post.对不起,很长的帖子。 I have been trying to figure this out for days.几天来,我一直试图弄清楚这一点。 But, I'm stumped.但是,我难住了。 Python is not my native language. Python 不是我的母语。

I am pulling a list of dictionaries containing information about 1,400 hosts from an API.我正在从 API 中提取包含有关 1,400 台主机信息的字典列表。

I convert the json data to a python list of dictionaries.我将 json 数据转换为 python 字典列表。 I create a second list to be used to populate a new list of dictionaries, one with a subset of the information from the list pulled from the API.我创建了第二个列表,用于填充新的字典列表,其中包含从 API 中提取的列表中的信息子集。

I create a list of the keys I need the info from.我创建了一个我需要从中获取信息的键的列表。 Next, I create two for loops.接下来,我创建了两个 for 循环。 The first iterates through the original list of dictionaries and the second iterates through the list of keys that I want to put into the new list of dictionaries.第一个遍历字典的原始列表,第二个遍历我想要放入新字典列表的键列表。

If I add print statements in the two loops, I can confirm that I am iterating through the correct information that I am looking for and that this information is being added to the new list of dictionaries.如果我在两个循环中添加打印语句,我可以确认我正在遍历我正在寻找的正确信息,并且此信息正在添加到新的字典列表中。

Both lists of dictionaries, the list of keys, and a new dictionary ( to be used in the loop ) are all defined as global in scope.字典列表、键列表和新字典(将在循环中使用)都在 scope 中定义为全局。

However, later in the script when I go to reference any specific element of the final list of dictionaries, all 1,400 dictionaries contain the same values from the last entry of the original list of dictionaries.但是,稍后在脚本中,当我 go 引用最终字典列表的任何特定元素时,所有 1,400 个字典都包含与原始字典列表的最后一个条目相同的值。

host_info is a list of dictionaries pulled from an API host_info 是从 API 中提取的字典列表

host_fields is a list of keys that I want to parse from host_info host_fields 是我想从 host_info 解析的键列表

# New list of dictionaries. We will populate the keys in these
# from the host_fields list above.
export_list_of_dictionaries = []

# New dictionary for use in populating in export_list_of_dictionaries
new_host = {}

# Loop through the host_info list of dictionaries to pull
# the specific host_fields
for index in  range(len(host_info)):
    for field in host_fields:
        # Add the field as a key to the new_host dictionary
        new_host[field] = host_info[index][field]

    # **** The line above is cycling through the fields of host_fields correctly ****

# print(index) **** the index is cycling through host_info correctly ****
# Add the new_host dictionary to the new export_list_of_dictionaries
export_list_of_dictionaries.append(new_host)

# **** The print statement below shows that each of the elements has the correct ip
#print(export_list_of_dictionaries[index]['ip'])
# print(len(export_list_of_dictionaries)) **** This printed the correct number of elements ****

The keys from the original list of dicts print correctly.原始字典列表中的键打印正确。 Each IP from host_info is different. host_info 中的每个 IP 都是不同的。

# Print the IP for the first element in each list of dictionary
print("IP from the first element of the original list of dictionaries")
print(host_info[0]['ip'])
print(host_info[1]['ip'])
print(host_info[-1]['ip'])

Here's where the trouble becomes apparent: However, the keys from the final list of dicts all have the same IP, which is incorrect.这就是问题显而易见的地方:但是,最终字典列表中的键都具有相同的 IP,这是不正确的。

print("IP from the first element of the final list of dictionaries")
print(export_list_of_dictionaries[0]['ip'])
print(export_list_of_dictionaries[1]['ip'])
print(export_list_of_dictionaries[-1]['ip'])

Simple answers only please, I'm new to Python.请只回答简单的问题,我是 Python 的新手。

It seems like all entries in your list are references to the new_host object, which you're modifying on every loop.您列表中的所有条目似乎都是对new_host object 的引用,您在每个循环中都对其进行了修改。 Try something like:尝试类似:

for index in  range(len(host_info)):
    # Add a new blank dict to the list
    export_list_of_dictionaries.append({})
    for field in host_fields:
        # Add the field as a key to the new element of the list
        export_list_of_dictionaries[index][field] = host_info[index][field]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM