TypeError:不可散列的类型:Networkx随机游走代码中以前起作用的"dict" [英] TypeError: unhashable type: 'dict' in Networkx random walk code that was previously working

查看:126
本文介绍了TypeError:不可散列的类型:Networkx随机游走代码中以前起作用的"dict"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为是可以在networkx节点/边缘图上进行随机游动的.
我最近决定使用导航器更新Anaconda,然后又回来运行我的程序,突然它停止了工作.相反,我现在得到错误代码:

I had what I thought was a working random walk over a networkx node/edge graph.
I recently decided to update Anaconda using the navigator and then I came back to run my program again and all of a sudden it has stopped working. Instead, I now get the error code:

runfile('C:/Users/e17/.spyder-py3/temp.py', wdir='C:/Users/e17/.spyder-py3')
Traceback (most recent call last):

File "<ipython-input-64-51811f4d02fc>", line 1, in <module>
runfile('C:/Users/e17/.spyder-py3/temp.py', wdir='C:/Users/e17/.spyder-py3')

File "C:\Users\e17\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 688, in runfile
execfile(filename, namespace)

File "C:\Users\e17\Anaconda3\lib\site- 
packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/e17/.spyder-py3/temp.py", line 19, in <module>
if rc in NodesVisited:

TypeError: unhashable type: 'dict'  

我一直在使用while循环来填充随机漫游过程中访问的节点来填充字典.字典可以借以进行大量的下游分析,真是太好了.我现在完全不知道该如何解决此问题.我认为我需要为字典提供一个密钥,但是我不确定在提供密钥后如何填充字典.无论如何,这是代码:

I had been using a while loop to fill a dictionary using the nodes that were visited during the random walk. This was really nice in that the dictionary lent itself to a lot of downstream analysis. I am now completely lost in how to fix this. I gather that I need to provide a key for the dictionary, but I'm not quite sure how to populate the dictionary after providing the keys. Anyway, here is the code:

import networkx as nx
import random


G_fb = nx.karate_club_graph()
counter = 0
loops = 1


mylist = [];
#run while loop for multiple attempts at random walker
while loops <= 1000:

    rc = random.choice(G_fb.nodes())
    NodesVisited = {}

    #Execute random walk
    while counter <= 11:
        if rc in NodesVisited:
                NodesVisited[rc] += 1
        else:
                NodesVisited[rc] = 1

        Neighbors = G_fb.neighbors(rc)
        rc = random.choice(Neighbors)
        counter += 1

    #Organize the node list in most visited with decreasing order
    MostVisited = sorted(NodesVisited, key = NodesVisited.get,reverse = True)

    #Separate the top 10 most visited vertex
    top_top = MostVisited[:10]

    #create a list of most visited sites for plotting for each iteration  
    mylist.append(top_top)


    loops = loops + 1
    counter = 0
print ('all done!')

非常感谢您的帮助!

推荐答案

在networkx 1.11中,G.nodes()list.在较新的版本中,它是一个NodeView对象.此更改是您遇到问题的原因.事实证明,从中进行随机选择将返回某个随机节点的数据,这是一个决定.

In networkx 1.11, G.nodes() is a list. In newer versions it is a NodeView object. This change is the cause of your problems. A random choice from it turns out to be returning some random node's data, which is a dict.

所以

if rc in NodesVisited

检查rc是否为NodesVisited的键.为此,它需要对rc进行哈希处理,但是它不再是节点,而是字典,因此不能.

checks whether rc is a key of NodesVisited. To do this, it needs to hash rc, but it's no longer a node, it's a dict, so it can't.

要解决此问题,请更改

rc = random.choice(G_fb.nodes())

rc = random.choice(list(G_fb.nodes()))

您同样需要更改

Neighbors = G_fb.neighbors(rc)

Neighbors = list(G_fb.neighbors(rc))

这篇关于TypeError:不可散列的类型:Networkx随机游走代码中以前起作用的"dict"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆