0

I have following list (generated after sequence of other functions computations):

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

I want to generate a flattened list [1, 0, 2, 1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 1, 2, 0, 0, 1, 2, 2, ....]. The elements may repeat, but are unequal length at the end (0 might be 10, 1 might be 30, ...).

When I run print([list(i) for i in componentVisited])

I get following error:

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

Any help to solve this mystery would be great.

lpt
  • 931
  • 16
  • 35
  • Possible duplicate of [Making a flat list out of list of lists in Python](https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python) – BENY Oct 01 '18 at 00:09
  • No. Those have been tried – lpt Oct 01 '18 at 01:02
  • You'll need to tell us exactly what `componentVisited` is. I don't know what kind of object would produce that error. The things I tried produce a `... object not iterable` error instead. – hpaulj Oct 01 '18 at 01:45
  • @hpaulj You can get this error in python 2 by passing a custom class to the `list` constructor or a list comprehension: `class dummy: pass` and now `list(dummy())` or `[x for x in dummy()]`. – Paul Panzer Oct 01 '18 at 02:18
  • @lpt what do you get when you try `print([type(i) for i in componentVisited])`? If you still get the same error, what is the output of `type(componentVisited)`? – Paul Panzer Oct 01 '18 at 02:40
  • We need an [mcve]. I think `componentVisited` is not what you think it is. – Joel Oct 01 '18 at 12:29

0 Answers0