0

I have a dictionary in which it contains URLs as key and Open Ports of their as value. i.e. dict200 = {}

dict200 = {}
{'http://REDACTED1.com': [21, 22, 80, 443, 3306], 'http://www.REDACTED2.com': [80, 443]}

Now I have another dictionary with different content i.e. newerdict = {}

newerdict = {}
newerdict = {'Drupal , ': '7', 'Apache , ': '2.4.34'}

Now Please assume that the Apache server is being used in the redacted1, and Drupal is being used on redacted2.

Now What I want is, something like this:-

{'http://redacted1.com': [{'apache': '2.4.34' }], 'http://redacted2.com': [{'Drupal': '7'}]}

Hope this time I explained better. Looking for any reply.

EDIT:-

Somehow I am able to replace the positions of values, but now the problem I am facing is, I can't access the properties of dict within a dict.

Here is the full output,

http://redacted1.com : [{'\tApache (web-servers), ': '2.4.34'}]
http://redacted2.com : [{'\tDrupal (cms), ': '7'}]

But how could I print

Apache = 2.4.34

1 Answers1

0

I assume that you have done few typos, and you can do the formatting by your own. You need a mapping to solve this problem.

This will mostly solve your problem

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)

EDIT: Using ordereddict to retain insertion order for python version 3.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'])
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Hello! Thanks for answering, But how can I get the mapping dictionary? as you got that. I can get dict200, newerdict, but can make the dictionary like your mapping dictionary. That is the main doubt. – Utkarsh Agrawal Aug 15 '18 at 07:08
  • You need to make the mapping dictionary yourself, or you need to find any pattern for linking `dict200` and `newerdict`. Are the insertion order same? I mean *redacted1.com* is on the first place so it should have *Drupal* . or it is a data which you receive from somewhere else? – Arghya Saha Aug 15 '18 at 07:12
  • Is it possible to make the mapping dictionary without ourself if *redacted1.com* is on the first place and have *Drupal*? If it is possible, please change the code according to it. because then I will have clear picture/ – Utkarsh Agrawal Aug 15 '18 at 07:21
  • Yes then you can use ordereddict from the collections library , I can update the answer accordingly if you need it – Arghya Saha Aug 15 '18 at 07:22
  • Updated the answer using ordereddict – Arghya Saha Aug 15 '18 at 07:59
  • Okay Cool enough. But How will you access it? For example, I want to print '7' How can I? – Utkarsh Agrawal Aug 15 '18 at 08:10
  • Hi, I have only a little problem, can you suggest me something? I am editing my question on the EDIT portion please see. Thanks. – Utkarsh Agrawal Aug 15 '18 at 10:58
  • Hi, Would you like to see my another question regarding Unhassble type list even after passing it on tuple. Here is https://stackoverflow.com/questions/50610656/unhassable-type-list-even-after-passing-tuple – Utkarsh Agrawal Aug 16 '18 at 07:41
  • Kindly create a question. You edited the question into a completely different question, which is not recommended – Arghya Saha Aug 16 '18 at 07:45
  • Oh yes I know, But I don't have any option (when I go to ask, I got You have no permission to ask,) – Utkarsh Agrawal Aug 16 '18 at 07:46
  • Hi, https://stackoverflow.com/questions/52082123/this-code-of-python-is-working-very-slow this is my new question on selenium based. If you would like to help then, please. And this time you will get full info. Thank you. – Utkarsh Agrawal Aug 30 '18 at 04:32