I have multiple things that I want to associate with each other.
e.g, A,B and C
I want 'A' to give 'B' and 'B' to give 'C'. Currently, I could only think of creating two separate dictionaries. Following texttable output shows what I have in dictionaries.
Dictionary 'd':
+--------------------------------------+--------------------------------------+
| Key | Value |
+======================================+======================================+
| 3223612326 | ['192.168.249.132:47671>192.168.249. |
| | 133:80', '192.168.249.132:9065>192.1 |
| | 68.249.133:80', '192.168.249.132:626 |
| | 6>192.168.249.133:80'] |
+--------------------------------------+--------------------------------------+
| 3118051391 | ['192.168.249.132:10867>192.168.249. |
| | 133:80', '192.168.249.132:20275>192. |
| | 168.249.133:80', '192.168.249.132:37 |
| | 189>192.168.249.133:80'] |
+--------------------------------------+--------------------------------------+
Dictionary 'e':
+------------------------------------------+-------+
| Key | Value |
+==========================================+=======+
| 192.168.249.132:20275>192.168.249.133:80 | ll |
+------------------------------------------+-------+
| 192.168.249.132:9065>192.168.249.133:80 | ll |
+------------------------------------------+-------+
| 192.168.249.132:47671>192.168.249.133:80 | He |
+------------------------------------------+-------+
| 192.168.249.132:37189>192.168.249.133:80 | o |
+------------------------------------------+-------+
| 192.168.249.132:10867>192.168.249.133:80 | He |
+------------------------------------------+-------+
| 192.168.249.132:6266>192.168.249.133:80 | o |
+------------------------------------------+-------+
As you can see, dictionary 'e' uses every value in dictionary 'd' as its key. This creates a lot of issues for me since I have to link everything between two different dictionaries. Is there a better way to achieve this in python ? Using dictionary or another container.
UPDATE
The code used for adding things to dictionary 'd' is something like:
def dictionaryd(sip, sport, dip, dport, key):
d = dict()
value = str(sip) + ":" + str(sport) + ">" + str(dip)+ ":" + str(dport)
if key in d:
if value not in d[key]:
d[key].append(value)
else:
d[key] = [value]
With the elements you're giving, it looks like values of the e dictonary are unique for
each key, meaning that you can use it within a tuple:
{ 3223612326 : [('192.168.249.132:20275>192.168.249.133:80', 'll'),
('192.168.249.132:9065>192.168.249.133:80', 'll'),
('192.168.249.132:6266>192.168.249.133:80', 'He')],
3118051391 : [('192.168.249.132:10867>192.168.249.133:80', 'o'),
('192.168.249.132:20275>192.168.249.133:80', 'He'),
('192.168.249.132:37189>192.168.249.133:80', 'o')]
}
If you want something slightly more convenient, you could use a NamedTuple:
from collections import namedtuple
RouteEntry = namedtuple('RouteEntry', ['route', 'comment'])
{ 3223612326 : [RouteEntry(route='192.168.249.132:20275>192.168.249.133:80', comment='ll'),
RouteEntry(route='192.168.249.132:9065>192.168.249.133:80', comment='ll'),
RouteEntry(route='192.168.249.132:6266>192.168.249.133:80', comment='He')],
3118051391 : [RouteEntry(route='192.168.249.132:10867>192.168.249.133:80', comment='o'),
RouteEntry(route='192.168.249.132:20275>192.168.249.133:80', comment='He'),
RouteEntry(route='192.168.249.132:37189>192.168.249.133:80', comment='o')]
}
Here would be my take at your problem, I certainly made assumptions, like the e table key being the timestamp at the execution of the function. This is why for the test case, I use a time.sleep(1) to have two arrows in the route_table.
I also tried to interpret your data, which looks like a routing table, always avoid using e, d and such names in a program, and always try to use relevant names in order for your readers to understand what you're doing.
import time
from collections import namedtuple
SourceAddress = namedtuple('SourceAddress', ['ip', 'port'])
DestinationAddress = namedtuple('DestinationAddress', ['ip', 'port'])
RouteEntry = namedtuple('RouteEntry', ['source', 'destination', 'comment'])
def save_routes(table, sip, sport, dip, dport, key):
src = SourceAddress(sip, sport)
dst = DestinationAddress(dip, dport)
entry = RouteEntry(src, dst, key)
table.setdefault(int(time.time()), []).append(entry)
route_table = {}
save_routes(route_table, '192.168.249.132', '20275', '192.168.249.133', '80', 'll')
save_routes(route_table, '192.168.249.132', '9065', '192.168.249.133', '80', 'll')
save_routes(route_table, '192.168.249.132', '6266', '192.168.249.133', '80', 'He')
time.sleep(1)
save_routes(route_table, '192.168.249.132', '10867', '192.168.249.133', '80', 'o')
save_routes(route_table, '192.168.249.132', '20275', '192.168.249.133', '80', 'He')
save_routes(route_table, '192.168.249.132', '37189', '192.168.249.133', '80', 'o')
My question is more on the general level of how do we link 3 things using maybe 1 dictionary
the answer to that kind of problematics is usually use a tuple or use a class instance. The real question in the end is how will you use your data, and how can you optimize the build and the reading of the data depending on your dataset.
All in all, your issue is not really a python problem it's a general one.
HTH
You have a tree-like data structure, thus you should use a tree. This is just a brief example.
class Node(object):
def __init__(self, label):
self._label = label
self._connections = {}
def __getitem__(self, item):
return self._connections[item]
def getlabel(self):
return self._label
def add_connection(self, node):
self._connections[node.getlabel()] = node
def __repr__(self):
return repr(self._connections.keys())
A = Node(3223612326)
B = Node('192.168.249.132:47671>192.168.249.133:80')
C = Node('ll')
A.add_connection(B)
B.add_connection(C) # it's equal to A['192.168.249.132:47671>192.168.249.133:80'].add_connection(C)
print(A['192.168.249.132:47671>192.168.249.133:80'])
print(A)
Output
['ll']
['192.168.249.132:47671>192.168.249.133:80']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With