我正在尝试将值附加到列出元素的字典中。我正在尝试从列表中提取这些值,但是它们在每个字典中只返回相同的值。
这是我的代码:
# Use a for loop to create a list of dictionaries with the associated information.
providers = ["Level3", "Verisign", "Google", "Quad9", "DNS.WATCH",
"Comodo Secure DNS", "OpenDNS Home", "Norton ConnectSafe",
"GreenTeamDNS", "SafeDNS", "OpenNIC", "SmartViper", "Dyn",
"FreeDNS", "Alternate DNS", "Yandex.DNS", "UncensoredDNS",
"Hurricane Electric", "puntCAT", "Neustar", "Cloudflare",
"Fourth Estate"]
ips = ["209.244.0.3", "64.6.64.6", "8.8.8.8", "9.9.9.9", "84.200.69.80",
"8.26.56.26", "208.67.222.222", "199.85.126.10", "81.218.119.11",
"195.46.39.39", "69.195.152.204", "208.76.50.50", "216.146.35.35",
"37.235.1.174", "198.101.242.72", "77.88.8.8", "91.239.100.100",
"74.82.42.42", "109.69.8.51", "156.154.70.1", "1.1.1.1", "45.77.165.194"]
#Use a for loop to create a dictionary mapping the provider names to their IPs.
dic = {k:v for k,v in zip(providers, ips)}
print(dic["Hurricane Electric"])
#Print Hurricane Electric's IPs using the dictionary.
#Use a for loop to create a list of dictionaries with the associated information.
#create dictionary within list_of_dicts
list_of_dicts = [{} for isp in providers]
print(list_of_dicts)
for dicts in list_of_dicts:
#append k:v
#loop through lists to pull values
for k,v in dic.items():
dicts["provider"] = [k]
print(list_of_dicts)
这是我的输出:
74.82.42.42
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
[{'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}, {'provider': ['Neustar']}]
预期输出:
[{'provider_name': 'Level3'}, {'primary_server': '209.244.0.3'}, ...]
答案 0 :(得分:1)
我认为这可以满足您的要求
# Use a for loop to create a list of dictionaries with the associated information.
providers = ["Level3", "Verisign", "Google", "Quad9", "DNS.WATCH",
"Comodo Secure DNS", "OpenDNS Home", "Norton ConnectSafe",
"GreenTeamDNS", "SafeDNS", "OpenNIC", "SmartViper", "Dyn",
"FreeDNS", "Alternate DNS", "Yandex.DNS", "UncensoredDNS",
"Hurricane Electric", "puntCAT", "Neustar", "Cloudflare",
"Fourth Estate"]
ips = ["209.244.0.3", "64.6.64.6", "8.8.8.8", "9.9.9.9", "84.200.69.80",
"8.26.56.26", "208.67.222.222", "199.85.126.10", "81.218.119.11",
"195.46.39.39", "69.195.152.204", "208.76.50.50", "216.146.35.35",
"37.235.1.174", "198.101.242.72", "77.88.8.8", "91.239.100.100",
"74.82.42.42", "109.69.8.51", "156.154.70.1", "1.1.1.1", "45.77.165.194"]
#Use a for loop to create a dictionary mapping the provider names to their IPs.
#create dictionary within list_of_dicts
list_of_dicts = []
for k, v in zip(providers, ips):
list_of_dicts.extend([{'provider': k}, {'primary_server': v}])
print(list_of_dicts)
输出:
[{'provider': 'Level3'}, {'primary_server': '209.244.0.3'}, {'provider': 'Verisign'}, {'primary_server': '64.6.64.6'}, {'provider': 'Google'}, {'primary_server': '8.8.8.8'}, {'provider': 'Quad9'}, {'primary_server': '9.9.9.9'}, {'provider': 'DNS.WATCH'}, {'primary_server': '84.200.69.80'}, {'provider': 'Comodo Secure DNS'}, {'primary_server': '8.26.56.26'}, {'provider': 'OpenDNS Home'}, {'primary_server': '208.67.222.222'}, {'provider': 'Norton ConnectSafe'}, {'primary_server': '199.85.126.10'}, {'provider': 'GreenTeamDNS'}, {'primary_server': '81.218.119.11'}, {'provider': 'SafeDNS'}, {'primary_server': '195.46.39.39'}, {'provider': 'OpenNIC'}, {'primary_server': '69.195.152.204'}, {'provider': 'SmartViper'}, {'primary_server': '208.76.50.50'}, {'provider': 'Dyn'}, {'primary_server': '216.146.35.35'}, {'provider': 'FreeDNS'}, {'primary_server': '37.235.1.174'}, {'provider': 'Alternate DNS'}, {'primary_server': '198.101.242.72'}, {'provider': 'Yandex.DNS'}, {'primary_server': '77.88.8.8'}, {'provider': 'UncensoredDNS'}, {'primary_server': '91.239.100.100'}, {'provider': 'Hurricane Electric'}, {'primary_server': '74.82.42.42'}, {'provider': 'puntCAT'}, {'primary_server': '109.69.8.51'}, {'provider': 'Neustar'}, {'primary_server': '156.154.70.1'}, {'provider': 'Cloudflare'}, {'primary_server': '1.1.1.1'}, {'provider': 'Fourth Estate'}, {'primary_server': '45.77.165.194'}]
我认为,如果您为每个提供者创建一个字典,并且每个字段具有单独的键,则结果将更易于使用。
换句话说,是这样的:
[{'provider': 'Level3', 'primary_server': '209.244.0.3'},
{'provider': 'Verisign', 'primary_server': '64.6.64.6'},
{'provider': 'Google', 'primary_server': '8.8.8.8'},
{'provider': 'Quad9', 'primary_server': '9.9.9.9'},
{'provider': 'DNS.WATCH'}, 'primary_server': '84.200.69.80'},
{'provider': 'Comodo Secure DNS', 'primary_server': '8.26.56.26'},
...etc
]
或者,根据您要执行的操作,也许一个包含所有 的字典是这样的:
{'Level3': '209.244.0.3',
'Verisign': '64.6.64.6',
'Google': '8.8.8.8',
'Quad9': '9.9.9.9',
'DNS.WATCH': '84.200.69.80',
'Comodo Secure DNS': '8.26.56.26',
...etc
}
答案 1 :(得分:0)
providers = ["Level3", "Verisign", "Google", "Quad9", "DNS.WATCH",
"Comodo Secure DNS", "OpenDNS Home", "Norton ConnectSafe",
"GreenTeamDNS", "SafeDNS", "OpenNIC", "SmartViper", "Dyn",
"FreeDNS", "Alternate DNS", "Yandex.DNS", "UncensoredDNS",
"Hurricane Electric", "puntCAT", "Neustar", "Cloudflare",
"Fourth Estate"]
ips = ["209.244.0.3", "64.6.64.6", "8.8.8.8", "9.9.9.9", "84.200.69.80",
"8.26.56.26", "208.67.222.222", "199.85.126.10", "81.218.119.11",
"195.46.39.39", "69.195.152.204", "208.76.50.50", "216.146.35.35",
"37.235.1.174", "198.101.242.72", "77.88.8.8", "91.239.100.100",
"74.82.42.42", "109.69.8.51", "156.154.70.1", "1.1.1.1", "45.77.165.194"]
dic = [{i[0]:i[1]} for i in zip(providers,ips)]
现在dic
是[{'Level3': '209.244.0.3'}, {'Verisign': '64.6.64.6'}, {'Google': '8.8.8.8'}, ...]
答案 2 :(得分:0)
也许这会有所帮助:
dic = [{k:v} for k,v in zip(providers,ips)]
答案 3 :(得分:0)
按索引迭代,不带zip。我还建议返回单个字典,例如:
res = {}
for i in range(len(providers)):
res[providers[i]] = ips[i]
#=> {'Level3': '209.244.0.3', 'Verisign': '64.6.64.6', ....
您可以通过以下方式轻松获取IP:
print(res['Hurricane Electric']) #=> 74.82.42.42
res_alt = []
for i in range(len(providers)):
res_alt.append({'provider_name':providers[i], 'primary_server': ips[i]})
print(res_alt)
#=> [{'provider_name': 'Level3', 'primary_server': '209.244.0.3'}, {'provider_name': 'Verisign', 'primary_server': '64.6.64.6'}, ...