Rya
Rya

Reputation: 329

Merge elements in tuples?

Here I have a dataset:

rd='''
1:A,B,C;D,E
2:F,G
3:H,J,K
'''

Desired result:

[('A','B'),('B',C'),('A','C'),('D','E'),('F','G'),('H','J'),('J','K'),('H','K')]

My 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 <j
                    j +=1
                    if j >len(edge):
                        end
                i +=1
                if i >len(edge)-1:
                    end
            allEdges.append(edge(i),edge(j))       

    return allEdges

I know the itertools 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.

Upvotes: 2

Views: 179

Answers (4)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

Below is the simplified solution to achieve it using re.compile() and itertools.combinations() functions. In order to flatten the list, I am using operator.add() with reduce() function:

import re
from itertools import combinations
from operator import add
rd='''
1:A,B,C;D,E
2:F,G
3:H,J,K
'''
my_aplhalist = (re.compile('(\n\d:)').split(rd.rstrip()))[2::2]
my_combinations = [list(combinations(item.split(','), 2)) for item_str in my_aplhalist for item in item_str.split(';')]
my_solution = reduce(add, my_combinations)
# Value of 'my_solution': [('A', 'B'), ('A', 'C'), ('B', 'C'), ('D', 'E'), ('F', 'G'), ('H', 'J'), ('H', 'K'), ('J', 'K')]

Upvotes: 1

trincot
trincot

Reputation: 350715

Here is how you could do it without import of itertools:

def rd_edges(f):
    allEdges =[]
    for line in f.split():
        edges = line.split(":")[1].split(';')
        for edge in edges:
            nodes = edge.split(',')
            for i, a in enumerate(nodes):
                for b in nodes[i+1:]:
                    allEdges.append((a,b))
    return allEdges

rd='''
1:A,B,C;D,E
2:F,G
3:H,J,K
'''
print (rd_edges(rd))

Upvotes: 2

Patrick Haugh
Patrick Haugh

Reputation: 61032

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

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.

Upvotes: 0

JulienD
JulienD

Reputation: 7293

The "Roughly equivalent to" code from the docs of the itertools function you have been wisely advised to use shows a correct version that is actually written in Python:

https://docs.python.org/3/library/itertools.html#itertools.combinations

Upvotes: 0

Related Questions