简体   繁体   English

如何处理python中的迭代器?

[英]How to deal with iterators in python?

Hi everybody I am trying to create a list (pd.Series) with one future obtained by using nx.jaccard_coefficient, the problem is that I am getting an iterator and I have no idea how to extract the result that I am supposed to obtain.大家好,我正在尝试使用 nx.jaccard_coefficient 获得的一个未来创建一个列表 (pd.Series),问题是我得到了一个迭代器,我不知道如何提取我应该获得的结果。 The function is supposed to return a iterator with 3 parameters u,v,p.该函数应该返回一个带有 3 个参数 u,v,p 的迭代器。

So I created a comprenhension list to extract the data for some set of edges.所以我创建了一个理解列表来提取一些边集的数据。

 future_connections["Jaccard"] = [nx.jaccard_coefficient(G,edge) for edge in 
                                  future_connections['index']]

And I get the following retults我得到以下结果

            Future Connection       index    a    b  Common_Neighbors  \
(6, 840)                  0.0    (6, 840)    6  840                 9   
(4, 197)                  0.0    (4, 197)    4  197                 2   
(620, 979)                0.0  (620, 979)  620  979                 0   
(519, 872)                0.0  (519, 872)  519  872                 2 
               Jaccard  
(6, 840)    <generator object jaccard_coefficient.<locals>...  
(4, 197)    <generator object jaccard_coefficient.<locals>...  
(620, 979)  <generator object jaccard_coefficient.<locals>...  
(519, 872)  <generator object jaccard_coefficient.<locals>...  

Any idea on how to extract the values of the generator???关于如何提取生成器的值的任何想法???

Thanks.谢谢。

If your function nx.jaccard_coefficient returns a tuple/list of 3 elements, try this.如果你的函数nx.jaccard_coefficient返回一个包含 3 个元素的元组/列表,试试这个。

future_connections["Jaccard"] = [
  tuple(nx.jaccard_coefficient(G,edge)) for edge in  future_connections['index']]

请尝试以下操作:

future_connections["Jaccard"] = [p for u,v,p in nx.jaccard_coefficient(G, future_connections.index)]

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

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