Python 如何使用字典关联多个事物?
我有很多东西想相互联系 e、 g、A、B和C 我要A给B,B给C。目前,我只能考虑创建两个单独的词典。下面的texttable输出显示了字典中的内容 字典“d”:Python 如何使用字典关联多个事物?,python,dictionary,Python,Dictionary,我有很多东西想相互联系 e、 g、A、B和C 我要A给B,B给C。目前,我只能考虑创建两个单独的词典。下面的texttable输出显示了字典中的内容 字典“d”: +--------------------------------------+--------------------------------------+ | Key | Value | +=======
+--------------------------------------+--------------------------------------+
| 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]
对于给定的元素,
e
dictional的值对于
每个键
,意味着您可以在元组中使用它:
{ 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')]
}
如果您想要更方便一点的东西,可以使用名为tuple的:
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\u表中有两个箭头
我还试图解释您的数据,它看起来像一个路由表,总是避免在程序中使用e
、d
等名称,并且总是尝试使用相关名称,以便您的读者了解您在做什么
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')
我的问题更多的是关于如何使用一本字典将三件事联系起来的一般层面
这类问题的答案通常是使用元组或类实例。最终真正的问题是如何使用数据,以及如何根据数据集优化构建和数据读取
总而言之,您的问题并不是真正的python问题,而是一般问题
HTH您有一个类似于树的数据结构,因此应该使用树。这只是一个简单的例子
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']
我以前在python中没有找到这种类型的字典。这是texttable的输出。字典是你的普通字典。您想让我添加关于如何创建字典的明显代码吗?只需使用嵌套字典。@user2061944添加准确的代码以及预期的输出。我同意的建议,但如果不知道您程序中的数据流量,很难做到规范性…谢谢。你的回答有帮助