Rohit Rohit - 16 days ago 7
Python Question

Python - Creating dictionary with list as values

I have a bind file in below format

BIND REQ conn=8349228 op=0 msgID=1 version=3 type=SIMPLE dn="uid=test1,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349229 op=0 msgID=1 version=3 type=SIMPLE dn="uid=test1,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349230 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349231 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349232 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349233 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349235 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349234 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349236 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349237 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349238 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349239 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349240 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349241 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349242 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"`


Now what i am trying to do is create a dictionary of the format
{'uid' : [connections ids]}
, for example like below

{'test1' : [8349228,8349229,...],
'xdev' : [8349230,8349231,...],
...so on }`


Can someone please guide me on this !! Any help is much appreciated.

Answer

These would be the steps:

Create empty dictionary 
Loop through lines in input file 
    Find values of `uid` and `conn` (easiest way using regex)
    Check if current `uid` exists in dictionary 
        It doesn't, create new entry with current `uid` as key and empty list as value.
    Append `conn` to list associated to `uid` key
Comments