How to get the outuput in the correct format
<p><img src="http://i.stack.imgur.com/ERBAv.png" alt="enter image description here">    </p>

<pre><code>def read_ndfa(file : open) -&gt; {str:{str:{str}}}:
    pass
</code></pre>

<p>file =</p>

<pre><code>start;0;start;1;start;0;near
near;1;end
end
</code></pre>

<p>correct output = <code>{'end': {}, 'start': {'1': {'start'}, '0': {'start', 'near'}}, 'near': {'1': {'end'}}}</code></p>

<p>I need to write a function that takes a file as an input to get the correct output as above. however, I have no idea how to create the inner dictionary by using zip function. can someone show me the correct way to do it? Many thanks.</p>

<p>You can take a look at this :</p>

<pre><code>import csv

f = open('data.csv', 'r')

r = csv.reader(f, delimiter=';')

out = {}

for row in r:
    out[row[0]] = {} # Set the top level key

    # Loop on all odd elements
    for pos in range(1, len(row), 2):

        if row[pos] in out[row[0]]:
            # Add the element to the existing set
            out[row[0]][row[pos]].add(row[pos+1])
        else:
            # Create a new set for the key
            out[row[0]][row[pos]] = set([row[pos+1]])

print(out)

f.close()
</code></pre>

<p>Which gives :</p>

<pre><code>{'near': {'1': {'end'}}, 'end': {}, 'start': {'0': {'near', 'start'}, '1': {'start'}}}
</code></pre>

python