我有一个字典,其中包含URL、作为键和Open的作为值。即dict200 = {}
dict200 = {}
{'http://REDACTED1.com': [21, 22, 80, 443, 3306], 'http://www.REDACTED2.com': [80, 443]}现在我有了另一本内容不同的字典,即newerdict = {}
newerdict = {}
newerdict = {'Drupal , ': '7', 'Apache , ': '2.4.34'}现在,请假定redacted1中使用的是Apache服务器,而redacted2上使用的是Drupal。
现在我想要的是,像这样的东西:-
{'http://redacted1.com': [{'apache': '2.4.34' }], 'http://redacted2.com': [{'Drupal': '7'}]}希望这次我解释得更清楚。寻求任何答复。
编辑:-
不知怎么的,我能够取代价值观的位置,但现在我面临的问题是,我不能访问dict的属性。
这是完整的输出,
http://redacted1.com : [{'\tApache (web-servers), ': '2.4.34'}]
http://redacted2.com : [{'\tDrupal (cms), ': '7'}]但我怎么能打印
Apache = 2.4.34发布于 2018-08-15 07:03:52
我假设您已经做了很少的排版,并且您可以按自己的格式进行格式化。您需要一个映射来解决这个问题。
这将主要解决你的问题
dict200 = {'http://redacted1.com': [21, 22, 80, 443, 3306], 'http://redacted2.com': [80, 443]}
newerdict = {'Drupal': '7', 'Apache': '2.4.34'}
mapping = {'http://redacted1.com': 'Apache', 'http://redacted2.com' : 'Drupal'}
new_output = dict()
for key, value in mapping.items():
new_output[key] = [{value: newerdict[value]}]
print(new_output)编辑:使用ordereddict为pythonVersion3.5保留插入顺序。Though python 3.7+ has it built in
from collections import OrderedDict
dict200 = OrderedDict({'http://redacted1.com': [21, 22, 80, 443, 3306], 'http://redacted2.com': [80, 443]})
newerdict = OrderedDict({'Drupal': '7', 'Apache': '2.4.34'})
dict200_index_wise = list(dict200.items())
newerdict_index_wise = list(newerdict.items())
new_output = dict()
for i in range(len(dict200)):
new_output[dict200_index_wise[i][0]] = [{newerdict_index_wise[i][0]:newerdict_index_wise[i][1]}]
print(new_output['http://redacted1.com'][0]['Drupal'])https://stackoverflow.com/questions/50484845
复制相似问题