需要 NetworkX 帮助

发布于 2024-09-15 13:56:07 字数 130 浏览 6 评论 0原文

目前我面临以下问题:

我有一个脚本可以搜索包含文档的特定目录。每个文档在文件名中都分配有一个编号。每个文档中的数字也代表另一个文档(文件名)。如何创建一个网络来显示哪些文档会导致什么结果?

任何帮助将不胜感激,谢谢

Currently im faced with the following problem:

I have a script that searches through a specific directory that contains documents. Each document is assigned a number within the filename. Within each document are numbers that also represent another document (filename). How can I create a web that shows what documents lead to what?

Any help would be appreciated, thanks

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

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

发布评论

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

评论(1

安穩 2024-09-22 13:56:07

这是有向图的教科书示例。您应该阅读 NetworkX 教程,以更好地了解如何使用它们;基本上,您需要添加所有节点(点),在本例中为文件编号,然后在它们之间添加边。

import os
import networkx as nx

g = nx.DiGraph( )
for filename in os.listdir( <dir> ):
    # do something to filename to get the number
    g.add_node( <number> )

for filename in os.listdir( <dir> ):
    # do something to filename to get the source
    with open( filename ) as theFile:
        # do something to theFile to get the targets
        for target in <targets>:
            g.add_edge( <source>, <target> )

import matplotlib.pyplot as plt
nx.draw( g )

This is a textbook example of a directed graph. You should read the NetworkX tutorial to get a better idea of how to work with them; basically, you need to add all the nodes (points), in this case file numbers, and then add edges between them.

import os
import networkx as nx

g = nx.DiGraph( )
for filename in os.listdir( <dir> ):
    # do something to filename to get the number
    g.add_node( <number> )

for filename in os.listdir( <dir> ):
    # do something to filename to get the source
    with open( filename ) as theFile:
        # do something to theFile to get the targets
        for target in <targets>:
            g.add_edge( <source>, <target> )

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