简体   繁体   中英

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. The function is supposed to return a iterator with 3 parameters 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.

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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