converting list to graph?
<p>suppose I have a 2D list of booleans such as:</p>

<pre><code>[[False, True , False],
 [True, False, False]]
</code></pre>

<p>would it be possible to convert this into a graph such that all the adjacent vals of the list are connected to each other?  adjacency meaning just another value either directly next to, above/under, or one diagonal space away from each other. I'm working on a problem that I think would be easier to accomplish with a graph but my input is a list and I was curious if there was a way of doing that?</p>

<p>What exactly do you mean by 'convert into a graph'?</p>

<p>One possibility is for you to create a structure for representing the graph. I suggest you read the top answer to this question: <a href="http://stackoverflow.com/questions/19472530/representing-graphs-data-structure-in-python">Representing graphs (data structure) in Python</a></p>

<p>Then, you create the proper connections and create the graph.</p>

<p>This code assumes all sublists are of equal length and graph is undirected.</p>

<pre><code>def getConnections(input_list):
    connections = []
    directions = [(-1,-1),(0,-1),(1,-1),(1,0)]
    for i in range(0,len(input_list)):
        for j in range(0,len(input_list[0])):
            for x,y in directions:
                if (i+y &gt;= 0 and i+y &lt; len(input_list) and j+x &gt;= 0 and j+x &lt; len(input_list))
                    pair = (input_list[i][j], input_list[i+y][j+x])
                    connections.append(pair)
    return connections

myList = [[False, True , False], [True, False, False]]
connections = getConnections(myList)
myGraph = new Graph(connections, directed = false)
</code></pre>

python list python-2.7 graph