小编Joe*_*oep的帖子

Networkx邻居设置不打印

我的networkx代码有点问题.我试图从图中的节点找到所有邻居,但....

neighbor = Graph.neighbors(element)
print(neighbor)
Run Code Online (Sandbox Code Playgroud)

输出:

<dict_keyiterator object at 0x00764BA0>
Run Code Online (Sandbox Code Playgroud)

而不是我应该得到的所有邻居...我的朋友,使用旧版本的networkx没有得到这个错误,他的代码是完全相同的,并且工作完美.

谁能帮我?降级我的networkx不是一个选择.

编辑:

这是我的完整代码

Graph = nx.read_graphml('macbethcorrected.graphml')    
actors = nx.nodes(Graph)

for actor in actors:
    degree = Graph.degree(actor)
    neighbor = Graph.neighbors(actor)
    print("{}, {}, {}".format(actor, neighbor, degree))
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的图表:http: //politicalmashup.nl/new/uploads/2013/09/macbethcorrected.graphml

graph nodes networkx python-3.x

6
推荐指数
1
解决办法
3652
查看次数

随机删除列表中的项目百分比

我有一个n元素列表.现在我必须删除此列表中所有元素的百分比.删除的元素必须全部随机挑选.

例如,设n = 0,2(要删除20%)

n = 0.2
list = [one, two, three, four, five, six, seven, eight, nine, ten]
Run Code Online (Sandbox Code Playgroud)

随机删除20%后,列表将是:

list = [one, three, four, five, seven, eight, nine, ten] # two and seven deleted
Run Code Online (Sandbox Code Playgroud)

现在你可能会想,嗯,这不是那么难......嗯,这一切都必须在一条线上完成,我对那些oneliners来说是新手......

因此,必须创建一个新函数,它返回相同的列表但没有元素的百分比:

def remove(n, list):
    return list # But the new list with elements removed
Run Code Online (Sandbox Code Playgroud)

请帮忙,返回后面输入什么?

python python-3.x

1
推荐指数
1
解决办法
593
查看次数

标签 统计

python-3.x ×2

graph ×1

networkx ×1

nodes ×1

python ×1