如何使用字典关联多个事物?

时间:2015-03-04 13:00:16

标签: python dictionary

我有很多想要彼此联​​系的事情。

例如,A,B和C

我想要' A'给予' B'和' B'给予C'。目前,我只能想到创建两个单独的词典。以下texttable输出显示我在词典中的内容。

词典' 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']             |
+--------------------------------------+--------------------------------------+

词典' 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     |
+------------------------------------------+-------+

正如你所看到的,词典' e'使用字典中的每个值' d'作为关键。这给我带来了很多问题,因为我必须在两个不同的词典之间链接所有内容。有没有更好的方法在python中实现这一点?使用字典或其他容器。

更新

用于向字典添加内容的代码' d'是这样的:

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]

2 个答案:

答案 0 :(得分:2)

使用您提供的元素,看起来e dictonary的值是唯一的 每个key,意味着您可以在元组中使用它:

{ 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')]
}

如果您想要更方便一些,可以使用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')]
}

这是我对你的问题的看法,我当然做了一些假设,比如e表键是执行函数的时间戳。这就是为什么对于测试用例,我使用time.sleep(1)route_table中有两个箭头。

我还尝试解释您的数据,看起来像路由表,始终避免在程序中使用ed和此类名称,并始终尝试使用相关名称以便读者要了解你在做什么。

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')

  

我的问题更多的是关于我们如何使用可能1字典链接3件事的一般水平

这类问题的答案通常是使用元组使用类实例。最后的真正问题是您将如何使用数据,以及如何根据数据集优化构建和读取数据。

总而言之,你的问题并不是一个普遍的问题。

HTH

答案 1 :(得分:1)

您有一个树状数据结构,因此您应该使用树。这只是一个简短的例子。

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)

输出

['ll']
['192.168.249.132:47671>192.168.249.133:80']