简体   繁体   English

如何从 python networkX 中的列表列表中访问元素? 类型错误:'int' object 不可迭代

[英]How to access an element from a list of lists in python networkX? TypeError: 'int' object is not iterable

I have an output from my loop, where G is a Graph from networkX.我的循环中有一个 output,其中 G 是来自 networkX 的图。

for node in G.nodes():
    start_end = [(node,k) for k,v in nx.shortest_path_length(G, node).items() if v == d]
    print (start_end)

which is lots of list classes with tuples inside them.这是很多列表类,里面有元组。 (if it was just one list, it would be easy to do start_end[0][0].) (如果只是一个列表,很容易做 start_end[0][0]。)

<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
....
[]
[]
[]
[]
[]
[]
[('45', '27'), ('45', '26'), ('45', '39'), ('45', '24'), ('45', '81'), ('45', '29'), ('45', '46'), ('45', '51'), ('45', '23'), ('45', '8'), ('45', '60'), ('45', '83'), ('45', '86'), ('45', '149'), ('45', '18'),  ('45', '99'), ('45', '78'), ('45', '120'), ('45', '134'), ('45', '121'), ('45', '122')]
[]
[]
[]
[]
[]
[]
[('129', '134')]
[]
[('134', '92'), ('134', '97')]
[]
[]
[]
[]
[]

I want to grab the first element of the longest list '45', in this example.在本例中,我想获取最长列表“45”的第一个元素。 I have tried to sort the lists by length.我试图按长度对列表进行排序。

sorted(start_end, reverse=True)
max(start_end)

which yields an error这会产生错误

#TypeError: 'int' object is not iterable

I have also tried我也试过

start_end = len([(node,k) for k,v in nx.shortest_path_length(G_sc, node).items() if v == d])
print(max(current))

with the same error.有同样的错误。

Below is the pseudo-code that you can reproduce.以下是您可以重现的伪代码。 here, how can I access the tuple ((2, 1) from the second (longest) list?在这里,我如何从第二个(最长的)列表中访问元组 ((2, 1) ?

In: 
import networkx as nx
G = nx.DiGraph()

G.add_edge(1,2); G.add_edge(1,4)
G.add_edge(3,1); G.add_edge(3,4)
G.add_edge(2,3); G.add_edge(4,3)

for node in G.nodes():
    start_end_nodes = [(node, k) for k,v in nx.shortest_path_length(G, 
                      node).items() if v == 2]
    
    print(start_end_nodes)

Out:
[(1, 3)]
[(2, 1), (2, 4)]
[(4, 1)]
[(3, 2)]

One way to solve this issue is to store all the start_end values in a list - start_ends - and then getting the max based on len :解决此问题的一种方法是将所有start_end值存储在列表中 - start_ends - 然后根据len获取最大值:

max(start_ends, key=len)

full code完整代码

def f2(G):
    start_ends = []
    d = 2
    for node in G.nodes():
        start_end = [(node, k) for k,v in nx.shortest_path_length(G, node).items() if v == d]
        start_ends.append(start_end)
        
    #print(max(start_ends, key=len)[0])
    return max(start_ends, key=len)[0]

For your graph:对于您的图表:

#In:
G = nx.DiGraph()

G.add_edge(1,2); G.add_edge(1,4)
G.add_edge(3,1); G.add_edge(3,4)
G.add_edge(2,3); G.add_edge(4,3)

f2(G)

#Out:
(2, 1)

Option 2:选项 2:

You could also use sorted for the same effect but I'd recommend using it if you want the list sorted and not just the max.您也可以使用sorted来获得相同的效果,但如果您希望列表排序而不仅仅是最大值,我建议您使用它。

# to sort use:
start_ends_sorted = sorted(start_ends, key=len, reverse=True)

# to get the same result as before:
start_ends_max = start_ends_sorted[0][0]

# ---------------
# In your example:
start_ends_sorted = [[(2, 1), (2, 4)],
                     [(1, 3)],
                     [(4, 1)],
                     [(3, 2)]]
start_ends_max = (2, 1)

somebody suggested this version to me by creating an empty dictionary.有人通过创建一个空字典向我推荐了这个版本。 so I'm trying to understand how this works, more specifically, the line 'scores[node] = end_node, and if its better(faster) than yours.所以我试图了解它是如何工作的,更具体地说,'scores[node] = end_node 行,以及它是否比你的更好(更快)。

def f2(G):        
    scores = {}
    d = 2
            
    for node in G.nodes():
        shortest = nx.shortest_path_length(G_sc, node)
        end_node = len([k for k,v in shortest.items() if v == d])
        scores[node] = end_node
    return ((max(scores))

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

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