拼合列表时出现非序列错误

时间:2018-09-30 23:53:52

标签: numpy networkx

我有以下列表(在计算完其他函数之后生成):

[1, 0, 2]
[1, 0, 2]
[0]
[1]
[2]
[1, 0, 2]
[0]
[1]
[2]
[1, 2, 0]
[0, 1]
[2]
[0, 2, 1]
[0, 1, 2]
[0, 1, 2]
[2, 0]
[1]
[2, 0]
[1]

我想生成一个扁平化列表[1, 0, 2, 1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 1, 2, 0, 0, 1, 2, 2, ....]。元素可以重复,但结尾的长度不相等(0可能是10,1可能是30,...)。

我运行print([list(i) for i in componentVisited])

我收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-466-0a019fbab5b6> in <module>()
     84 numVertices=3
     85 xs = linspace(xstart, xend, num=xpts)
---> 86 data = graphLargestComponentSize(numVertices, xs)
     87 #data[:20]
     88 

<ipython-input-466-0a019fbab5b6> in graphLargestComponentSize(n, theRange)
     72 
     73 def graphLargestComponentSize(n, theRange):
---> 74     return [(p, sizeOfLargestComponent(randomGraph(n, p))) for p in theRange]
     75 
     76 

<ipython-input-466-0a019fbab5b6> in sizeOfLargestComponent(vertices)
     68 def sizeOfLargestComponent(vertices):
     69 
---> 70     return max(len(c) for c in connectedComponents(vertices))
     71 
     72 

<ipython-input-466-0a019fbab5b6> in connectedComponents(vertices)
     42             components.append(componentVisited)
     43             cumulativeVisited |= componentVisited
---> 44             print([list(i) for i in  componentVisited])
     45             from itertools import chain
     46             newlist=[i for i in componentVisited]

TypeError: iteration over non-sequence

任何帮助解决这个难题的方法都将非常有用。

0 个答案:

没有答案