Aidis
Aidis

Reputation: 1280

Creating list that would contain pairs of another list?

Sorry for my inability to write a question in elegant way. I hope that further text will be sufficient to understand my question.

I have a set of lists that contain from one to five members. I would like to create graph from each list.

My data:

data = [
['07F', '05F', '10F', '06F'],
['05T', '05F', '02T'],
['03T', '03F']
]

I expect that each list would result in all possible pairs of list:

['07F', '05F', '10F', '06F'] should become ----------->

['07F',05F']
['07F',10F']
['07F',06F']
['05F',10F']
['05F',06F']
['10F',06F']

The same with all next lists.

All I understand is that I have to iterate through the list, and then..?

Upvotes: 1

Views: 57

Answers (2)

markcial
markcial

Reputation: 9323

import itertools    
print [a for a in itertools.combinations(data,2)]

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168626

import itertools
from pprint import pprint
data = [
    ['07F', '05F', '10F', '06F'],
    ['05T', '05F', '02T'],
    ['03T', '03F']
    ] 

# Create a list of the lists of pair-wise combinations
data = [list(itertools.combinations(x,2)) for x in data]

pprint(data)

Upvotes: 4

Related Questions