本文介绍了需要NetworkX的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正面临以下问题:

我有一个脚本可以搜索包含文档的特定目录。每个文档都在文件名中分配一个数字。在每个文档中都有代表另一个文档(文件名)的数字。我怎样才能创建一个网站,显示什么文件导致什么?



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

解决方案

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

 在os.listdir(< dir>)中输入os 
导入networkx为nx

g = nx.DiGraph()
作为文件名:
#在os.listdir(< dir>)中获取数字
g.add_node(< number>)

作为文件名:
#源文件
打开(文件名)作为文件:
#为文件做些什么来获取目标
用于<目标>中的目标:
g.add_edge(< source> ,< target>)

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


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

解决方案

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 )

这篇关于需要NetworkX的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:01