当前位置: 首页>>代码示例>>Python>>正文


Python Graph.node[g[0]][key]方法代码示例

本文整理汇总了Python中networkx.Graph.node[g[0]][key]方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.node[g[0]][key]方法的具体用法?Python Graph.node[g[0]][key]怎么用?Python Graph.node[g[0]][key]使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx.Graph的用法示例。


在下文中一共展示了Graph.node[g[0]][key]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: read

# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import node[g[0]][key] [as 别名]
def read(string):

    """
    Read a graph from a string in Dot language and return it. Nodes and
    edges specified in the input will be added to the current graph.
    
    Args:
        - string: Input string in Dot format specifying a graph.
    
    Returns:
        - networkx.Graph object
    """

    lines = string.split("\n")
    first_line = lines.pop(0)
    if not re.match("graph \w+ {$", first_line):
        raise WrongDotFormatException("string contains no parseable graph")

    re_node = re.compile('^"([^"]*)"(?: \[([^]]*)\])?;?$')
    re_edge = re.compile('^"([^"]*)" -- "([^"]*)"(?: \[([^]]*)\])?;?$')

    gr = Graph()

    for line in lines:
        line = line.strip()
        match_node = re_node.search(line)
        match_edge = re_edge.search(line)
        if match_node:
            g = match_node.groups()
            gr.add_node(g[0])
            if g[1]:
                for attribute_string in g[1].split(", "):
                    key, value = attribute_string.split("=")
                    if value == "True":
                        value = True
                    elif value == "False":
                        value = False
                    gr.node[g[0]][key] = value

        elif match_edge:
            g = match_edge.groups()
            gr.add_edge(g[0], g[1])
            if g[2]:
                for attribute_string in g[2].split(", "):
                    key, value = attribute_string.split("=")
                    gr.edge[g[0]][g[1]][key] = value

        elif line != "}" and len(line) > 0:
            raise WrongDotFormatException("Could not parse line:\n\t{0}".format(line))

    return gr
开发者ID:pombredanne,项目名称:qlc,代码行数:53,代码来源:translationgraph.py


注:本文中的networkx.Graph.node[g[0]][key]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。