Python 中的非顺序循环优化

发布于 2024-12-07 13:15:52 字数 558 浏览 0 评论 0原文

我有两个字典对象,连接和网络。这可以可视化为图表,其中每个节点都是一台计算机,连接描绘了计算机和节点之间的边缘。网络是唯一网络的字典对象,计算机可以是其中的一部分,例如

1,2  
2,3  
4,5  
5,1 

节点 1 到 1 的四个连接信息
因此,连接将是 {1->1,2->1,3->1,4->2,5->1}
和网络 {1->0,2->1}
这意味着
计算机 1,2,3,5 是 n/w 1 的一部分
计算机 4 是 n/w 2 的一部分
n/w 2 再次互连到 n/w 1
我必须读取一个包含数千个此类连接信息的文件
这样做时,对于每个连接信息读取,我必须执行一个非顺序循环,如下所示

while network.has_key(connections[node1]):
    connections[node1]=network[connections[node1]] 

是否有更好的方法来优化上述循环?如果需要,我可以为此目的分享我的整个代码

I have two dictionary objects, connections and network. This can be visualized as a graph, where each node is a computer and connections depict an edge between the computers and node. Network is a dictionary objects of unique networks where the computers can be a part, for ex

1,2  
2,3  
4,5  
5,1 

are four connection information for nodes 1 through 1
connections would thus be {1->1,2->1,3->1,4->2,5->1}
and network {1->0,2->1}
which means
computer 1,2,3,5 are part of n/w 1
computer 4 is part of n/w 2
again n/w 2 is interconnected to n/w 1
I have to read a file with thousand of such connection information
while doing so, for each connection info read I have to perform a non-sequential loop as follows

while network.has_key(connections[node1]):
    connections[node1]=network[connections[node1]] 

Is there a better way to optimize the above loop? If required I can share my entire code for the purpose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-12-14 13:15:52

我不太明白你的问题,但我觉得与图表相关的问题的标准答案适用于这里:

使用 NetworkX< /a> 而不是尝试提出自己的数据表示和算法。它可能更难学习,但从长远来看,它会节省你的时间和麻烦。

I'm didn't really understand your question, but I feel the stock answer to graph-related questions applies here:

Use NetworkX rather than trying to come up with your own data representations and algorithms. It may be harder to learn, but it'll save you time and headaches in the long run.

漫雪独思 2024-12-14 13:15:52
while network.has_key(connections[node1]):
    connections[node1]=network[connections[node1]] 

该代码对 network[connections[node1]] 进行两次查找,一次查找 has_key,一次查找 []

它还会重复访问connections[node1]

相反,您可以这样做:

current = connections[node1]
while True:
    try:
         current = network[current]
    except KeyError:
         break
connections[node1] = current

但是重新编写其余代码可能会得到更好的改进。但为此,您可能会发现 http://codereview.stackexchange.com 是一个更好的网站。

while network.has_key(connections[node1]):
    connections[node1]=network[connections[node1]] 

That code does the lookup for network[connections[node1]] twice once for has_key and once for [].

It also repeatedly accesses connections[node1]

Instead you can do:

current = connections[node1]
while True:
    try:
         current = network[current]
    except KeyError:
         break
connections[node1] = current

But chances are better improvements can be had be reworking the rest of your code. But for that you may find http://codereview.stackexchange.com to be a better site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文