本文整理汇总了Python中networkx.Graph.nodes_iter方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.nodes_iter方法的具体用法?Python Graph.nodes_iter怎么用?Python Graph.nodes_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.Graph
的用法示例。
在下文中一共展示了Graph.nodes_iter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import nodes_iter [as 别名]
n = graph.number_of_nodes()
i = 0
print('Apply shapefile')
for node in graph.nodes():
p = Point(graph.node[node]['lon'], graph.node[node]['lat'])
if not shape_file.contains(p):
graph.remove_node(node)
i += 1
print('{0}/{1} nodes processed'.format(i, n), end='\r')
print('{0}/{1} nodes processed'.format(i, n))
print('Search for orphaned nodes')
orphaned = set()
n = graph.number_of_nodes()
i = 0
for node in graph.nodes_iter():
if graph.degree(node) == 0:
orphaned.add(node)
i += 1
print('{0}/{1} nodes processed'.format(i, n), end='\r')
print('{0}/{1} nodes processed'.format(i, n))
print('Delete {0} orphaned nodes'.format(len(orphaned)))
graph.remove_nodes_from(orphaned)
print('Calculate offset')
points = [node[1]['pos'] for node in graph.nodes(data=True)]
min_x = min(points, key=lambda p: p[0])[0]
min_y = min(points, key=lambda p: p[1])[1]
for node in graph.nodes_iter():
pos = (graph.node[node]['pos'][0] - min_x, graph.node[node]['pos'][1] - min_y)
示例2: __init__
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import nodes_iter [as 别名]
class WishPool:
def __init__( self,
wishes,
min_edge_score = MIN_EDGE_SCORE,
# min_number_of_edges = MIN_NUMBER_OF_EDGES,
connector = default_connector ):
"""
A filterable pool of wishes represented as nodes. Is used to find
relations between wishes.
@param connector: default connector is jaccard
@return:
"""
self.graph = Graph()
self.min_edge_weight = min_edge_score
# self.min_number_of_edges = min_number_of_edges
for wish in wishes:
self.graph.add_node( wish )
# loops through the existing nodes in the graph
for other in self.graph.nodes_iter():
# compares candidate to all existing nodes except itself
if other != wish and other.person != wish.person:
score = connector( wish, other )
if score > self.min_edge_weight:
self.graph.add_edge( wish, other, weight=score )
## processes the graph, excludes lonely nodes
self.connected_nodes = self.update_connected_nodes()
debug_graph(self.graph, message="Connected nodes are set")
self.lonely_nodes = self.update_lonely_nodes()
self.update_cliques()
## evaluates alternatives, gathers suggestions for each wish
# this implies that some cliques occur twice
self.suggestions = self.update_suggestions()
def update_connected_nodes( self ):
connected = set()
for n in self.graph.edges():
connected.add( n[0] )
return connected
def update_lonely_nodes( self ):
whole_graph = set( self.graph.nodes() )
return whole_graph.difference( self.connected_nodes )
def remove_lonely_nodes( self ):
raise NotImplementedError
def update_cliques( self ):
result = set()
for c in find_cliques( self.graph ):
if len(c) > 1:
result.add( Clique(c) )
self.cliques = result
return result
def get_distributed_cliques( self ):
self.cliques_for_wish = {}
for n in self.graph.nodes():
clique_buffer = []
for c in self.cliques:
if n in c.nodes:
clique_buffer.append( c )
if len(clique_buffer):
self.cliques_for_wish[n.pk] = [
c for c in clique_buffer if len(c.nodes) > 1 ]
return self.cliques_for_wish
def get_conflicting_cliques( self ):
result = {}
for w,c in self.get_distributed_cliques():
if len(c) > 1:
result[w] = c
return result
def update_suggestions( self ):
suggestions = []
for wish_pk, cliques in self.get_distributed_cliques().items():
suggestion = Suggestion(
Wish.objects.get(pk=wish_pk), cliques )
suggestions.append( suggestion )
self.suggestions = suggestions
return suggestions
def create_groups( self ):
distinct_cliques = set()
for s in self.suggestions:
distinct_cliques.add( s.get_best_clique() )
for c in distinct_cliques:
c.create_group()
def get_suggestion_pool( self ):
return SuggestionPool( self.suggestions )