Merge elements in tuples?
<p>Here I have a dataset:</p>

<pre><code>rd='''
1:A,B,C;D,E
2:F,G
3:H,J,K
'''
</code></pre>

<p>Desired result:</p>

<pre><code>[('A','B'),('B',C'),('A','C'),('D','E'),('F','G'),('H','J'),('J','K'),('H','K')]
</code></pre>

<p>My code:</p>

<pre><code>def rd_edges(f):
    allEdges =[]
    for line in f.split():
        edges =line.split(":")[1].split(';')
        for edge in edges:
            i =0
            j =1
            for i in len(edge):
                for j in len(edge):
                    i &lt;j
                    j +=1
                    if j &gt;len(edge):
                        end
                i +=1
                if i &gt;len(edge)-1:
                    end
            allEdges.append(edge(i),edge(j))       

    return allEdges
</code></pre>

<p>I know the <code>itertools</code> module can solve this problem, but want to write a function to transfer the data into a tuple without importing any modules. I reviewed some past questions posted on the forum, but am still confused about doing this.</p>

<pre><code>def find_edges(f):    
    out = []
    for line in f.split():
      line = line.split(':')[1]
      disjoint = line.split(';')
      for d in disjoint:
        s = d.split(',')
        for i, node in enumerate(s)):
          for downnode in s[i+1:]:
            out.append((node, downnode))
    return out
</code></pre>

<p>This works, but gives out put in a different order.  If you care about that, you have to start at the end of the list of nodes and build towards it.</p>

python tuples