简体   繁体   English

循环提取Dict值

[英]pulling Dict values in a loop

I'm having trouble isolating the values of a graphs edge weight in networkx Im using kruskals algorithm to find a the MST of a given graph. 我在使用kruskals算法隔离networkx Im中的图边缘权重值以查找给定图的MST时遇到麻烦。 U and V are point in Euclidean space and data is a dictionary for the weight of each edge. U和V是欧几里得空间中的点,数据是每个边缘权重的字典。 I'm using the values function for dictionaries to store the value in the set however I am returned this error 我使用字典的values函数将值存储在集合中,但是返回此错误

  File "/Users/mac/anaconda3/lib/python3.6/site-packages/networkx/convert_matrix.py", line 1143, in to_numpy_array
  A[index[u], index[v]] = d.get(weight, 1)
  TypeError: float() argument must be a string or a number, not 'dict_values'

the particular piece of code I am having trouble with is this. 我遇到麻烦的特定代码是这个。

for u,v, data in sorted(self.original_graph.edges(data=True), key=lambda x: x[2]['weight']):
        setu = UF.find(u)
        setv= UF.find(v)

        if setu != setv:
            A.append((u,v,data.values()))
            UF.union(setu,setv)

I am fully aware that networkx has their own minimum spanning tree functions built in, however I am trying to learn these algorithms on my own instead before blindly implementing built in functions 我完全意识到networkx内置了自己的最小生成树函数,但是在尝试盲目实现内置函数之前,我尝试自己学习这些算法。

阅读python文档后,我意识到data.values()返回dict中所有值的列表,我需要做的就是引用诸如data ['weight']之类的键来获取它的值。持有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM