gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:34:08 27 4
gpt4 key购买 nike

我有很多东西想要相互关联。

例如,A、B 和 C

我希望“A”给“B”,“B”给“C”。目前,我只能想到创建两个单独的词典。以下文本表输出显示了我在字典中的内容。

字典“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]

最佳答案

根据您提供的元素,e 字典的值对于每个 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')

My question is more on the general level of how do we link 3 things using maybe 1 dictionary

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

总而言之,您的问题并不是真正的 python 问题,而是一般问题。

HTH

关于python - 如何使用字典关联多个事物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com