Python create many-to-many relationships from a list - python

I have a list, say terms = ['A', 'B', 'C', 'D']
Which is the best way to create a list-of-lists or list-of-tuples of many-to-many relationships like this;
[['A','B'],['A','C'],['A','D'],['B','C'],['B','D'],['C','D']]

Using itertools.combinations():
from itertools import combinations
list(combinations(terms, r=2))
Demo:
>>> from itertools import combinations
>>> terms = ['A', 'B', 'C', 'D']
>>> list(combinations(terms, r=2))
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
These are tuples, not lists, but that's easily remedied if that is a problem:
>>> map(list, combinations(terms, r=2))
[['A', 'B'], ['A', 'C'], ['A', 'D'], ['B', 'C'], ['B', 'D'], ['C', 'D']]

Related

How can I rearrange a set of values into new pattern on python and print results

so I will do my best to explain what I'm looking for,
at the moment I have a 100 item list that I want to repetitively shuffle using a set pattern to first check if the pattern will eventually bring me back to where I began
and 2 to print the result of each loop to a text file.
so using a 3 item list as my example
[a,b,c]
and the shuffle pattern [3 1 2]
where the 3rd item becomes the first.
the first item becomes the second
and the second item becomes the 3rd
on a loop would generate the following patterns
[a,b,c]
[3,1,2]
[c,a,b]
[b,c,a]
[a,b,c]
but I have a list at the moment of 100 items that I need to find every single arrangement for a few different patterns I would like to test out.
does anyone know of a way to do this in python please.
You can define function and call this function multi times like below:
>>> def func(lst, ptr):
... return [lst[idx-1] for idx in ptr]
>>> lst = ['a','b','c']
>>> ptr = [3,1,2]
>>> for _ in range(5):
... lst = func(lst, ptr)
... print(lst)
['c', 'a', 'b']
['b', 'c', 'a']
['a', 'b', 'c']
['c', 'a', 'b']
['b', 'c', 'a']
You could use numpy advanced integer indexing if your list contains a numeric type:
import numpy as np
original_list=[1,2,3]
numpy_array = np.array(original_list)
pattern = [2,1,0]
print(numpy_array[pattern])
>>> array([3, 2, 1])
def rearrange(pattern : list,L:list):
new_list = []
for i in pattern :
new_list.append(L[i-1])
return new_list
print(rearrange([3,1,2],['a','b','c']))
output :
['c', 'a', 'b']
Itertools could be what you need.
import itertools
p = itertools.permutations(['a','b','c', 'd'])
list(p)
Output:
[('a', 'b', 'c', 'd'),
('a', 'b', 'd', 'c'),
('a', 'c', 'b', 'd'),
('a', 'c', 'd', 'b'),
('a', 'd', 'b', 'c'),
('a', 'd', 'c', 'b'),
('b', 'a', 'c', 'd'),
('b', 'a', 'd', 'c'),
('b', 'c', 'a', 'd'),
('b', 'c', 'd', 'a'),
('b', 'd', 'a', 'c'),
('b', 'd', 'c', 'a'),
('c', 'a', 'b', 'd'),
('c', 'a', 'd', 'b'),
('c', 'b', 'a', 'd'),
('c', 'b', 'd', 'a'),
('c', 'd', 'a', 'b'),
('c', 'd', 'b', 'a'),
('d', 'a', 'b', 'c'),
('d', 'a', 'c', 'b'),
('d', 'b', 'a', 'c'),
('d', 'b', 'c', 'a'),
('d', 'c', 'a', 'b'),
('d', 'c', 'b', 'a')]
​

python get groups of combinations that each member appear only once

I'm trying to get groups of permutations/combinations (r=2) that each member appear only once
I used python 'combinations' package to have the combinations.
For example: the members are: a,b,c,d.
The combinations are: [a,b],[a,c],[a,d],[b,c],[b,d]...
My desired output is:
[ {[a,b],[c,d]},{[a,c],[b,d]},{[a,d],[b,c]}...]
I would like to know what is the terminology for that case and if there is already implementation for that.
Thanks.
Here's a way to do it:
from itertools import combinations, chain
l = ['a','b','c','d']
c = list(combinations(l,2))
[set(i) for i in list(combinations(c,2)) if (len(set(l) & set(chain(*i))) == len(l))]
[{('a', 'b'), ('c', 'd')}, {('a', 'c'), ('b', 'd')}, {('a', 'd'), ('b', 'c')}]
Explanation
You can use itertools.combinations twice, in order to get all the 2 tuple combinations from:
list(combinations(l,2))
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
And select only those whose set of elements intersect with that of the original list, len(set(l) & set(chain(*i))) == len(l)) for every possible combination.
You can find the permutations up to r and then filter the results:
def combinations(d, d1, r, _c = [], _start=[]):
if len([i for b in _c for i in b]) == len(d1):
yield _c
else:
for i in d:
if i not in _start:
if len(_start+[i]) < r:
yield from combinations(d, d1, r, _c, _start=_start+[i])
else:
_d = sorted(_start+[i])
yield from combinations([i for i in d if i not in _d], d1,r, _c+[_d], [])
data = ['a', 'b', 'c', 'd']
_r = list(combinations(data, data, 2))
new_d = [a for i, a in enumerate(_r) if a not in _r[:i]]
Output:
[[['a', 'b'], ['c', 'd']], [['a', 'c'], ['b', 'd']], [['a', 'd'], ['b', 'c']], [['b', 'c'], ['a', 'd']], [['b', 'd'], ['a', 'c']], [['c', 'd'], ['a', 'b']]]

Extracting Unique String Combinations from List of List in Python

I'm trying to extract all the unique combinations of strings from a list of lists in Python. For example, in the code below, ['a', 'b','c'] and ['b', 'a', 'c'] are not unique, while ['a', 'b','c'] and ['a', 'e','f'] or ['a', 'b','c'] and ['d', 'e','f'] are unique.
I've tried converting my list of lists to a list of tuples and using sets to compare elements, but all elements are still being returned.
combos = [['a', 'b', 'c'], ['c', 'b', 'a'], ['d', 'e', 'f'], ['c', 'a', 'b'], ['c', 'f', 'b']]
# converting list of list to list of tuples, so they can be converted into a set
combos = [tuple(item) for item in combos]
combos = set(combos)
grouping_list = set()
for combination in combos:
if combination not in grouping_list:
grouping_list.add(combination)
##
print grouping_list
>>> set([('a', 'b', 'c'), ('c', 'a', 'b'), ('d', 'e', 'f'), ('c', 'b', 'a'), ('c', 'f', 'b')])
How about sorting, (and using a Counter)?
from collections import Counter
combos = [['a', 'b', 'c'], ['c', 'b', 'a'], ['d', 'e', 'f'], ['c', 'a', 'b'], ['c', 'f', 'b']]
combos = Counter(tuple(sorted(item)) for item in combos)
print(combos)
returns:
Counter({('a', 'b', 'c'): 3, ('d', 'e', 'f'): 1, ('b', 'c', 'f'): 1})
EDIT: I'm not sure if I'm correctly understanding your question. You can use a Counter to count occurances, or use a set if you're only interested in the resulting sets of items, and not in their uniqueness.
Something like:
combos = set(tuple(sorted(item)) for item in combos)
Simply returns
set([('a', 'b', 'c'), ('d', 'e', 'f'), ('b', 'c', 'f')])
>>> set(tuple(set(combo)) for combo in combos)
{('a', 'c', 'b'), ('c', 'b', 'f'), ('e', 'd', 'f')}
Simple but if we have same elements in the combo, it will return wrong answer. Then, sorting is the way to go as suggested in others.
How about this:
combos = [['a', 'b', 'c'], ['c', 'b', 'a'], ['d', 'e', 'f'], ['c', 'a', 'b'], ['c', 'f', 'b']]
print [list(y) for y in set([''.join(sorted(c)) for c in combos])]

How to generate the permutation of a list in python? [duplicate]

This question already has answers here:
How do I generate all permutations of a list?
(40 answers)
Closed 8 years ago.
I am trying to generate a permutation of list in python with recursion.
import copy
def perm(seq):
if len(seq) == 1:
return seq
else:
nseq = perm(seq[1:])
return zip_letter(seq[0], nseq)
def zip_letter(c, seq):
lis = []
for i in range(len(seq)+1):
seq.insert(i, c)
lis.append(copy.deepcopy(seq))
seq.pop(i)
return lis
print perm(['a', 'b', 'c'])
The output is
[['a', ['b', 'c'], ['c', 'b']], [['b', 'c'], 'a', ['c', 'b']], [['b', 'c'], ['c', 'b'], 'a']]
which seems fine, but not in correctly inserted format.
what am i missing?
I would recommend using the existing function in itertools
>>> list(itertools.permutations(['a', 'b', 'c']))
[('a', 'b', 'c'),
('a', 'c', 'b'),
('b', 'a', 'c'),
('b', 'c', 'a'),
('c', 'a', 'b'),
('c', 'b', 'a')]

How to get all possible combinations of two different lists?

I'm having so much trouble understanding my issue:
I got 2 lists:
from = ['A', 'B', 'C']
to = ['D', 'E', 'F']
I need to produce a matrix that combines each item from one list to the other as such:
final = [[['A', 'D'], ['B', 'E'], ['C', 'F']],
[['A', 'D'], ['B', 'F'], ['C', 'E']],
[['A', 'E'], ['B', 'F'], ['C', 'D']],
[['A', 'E'], ['B', 'D'], ['C', 'F']],
[['A', 'F'], ['B', 'D'], ['C', 'E']],
[['A', 'F'], ['B', 'E'], ['C', 'D']]]
I was trying to do this with this:
for i in range(len(initial)):
for j in range(len(transformed)):
self.semantic_networks[j][i][0] = self.initial_figure[i]['name']
self.semantic_networks[i][j][1] = self.transformed_figure[(j + i) % len(self.transformed_figure)]['name']
But, I'm getting only the top:
[['A', 'D'], ['B', 'E'], ['C', 'F']]
[['A', 'E'], ['B', 'F'], ['C', 'D']]
[['A', 'F'], ['B', 'D'], ['C', 'E']]
[[0, 0], [0, 0], [0, 0]]
[[0, 0], [0, 0], [0, 0]]
[[0, 0], [0, 0], [0, 0]]
What am I trying to get? Combination? Permutation? Combination of combinations??
Any hints???
Apply itertools.permutations on the second list and then zip each permutation with first list.
from itertools import permutations
lst1 = ['A', 'B', 'C']
lst2 = ['D', 'E', 'F']
for p in permutations(lst2):
print zip(lst1, p)
#
[('A', 'D'), ('B', 'E'), ('C', 'F')]
[('A', 'D'), ('B', 'F'), ('C', 'E')]
[('A', 'E'), ('B', 'D'), ('C', 'F')]
[('A', 'E'), ('B', 'F'), ('C', 'D')]
[('A', 'F'), ('B', 'D'), ('C', 'E')]
[('A', 'F'), ('B', 'E'), ('C', 'D')]
It seems you want the combination of all possible permutations:
import itertools
a = ['A', 'B', 'C']
b = ['D', 'E', 'F']
items = zip(itertools.permutations(a), itertools.permutations(b))

Categories