S
S
schokk64rus2021-05-01 13:27:25
Python
schokk64rus, 2021-05-01 13:27:25

How to generate all possible combinations from two lists?

Good afternoon!
There are two lists:

atoms = ['Co1', 'Co2', 'Co3', 'Co4', 'Co5', 'Co6', 'Co7', 'Co8']
dopedatoms = ['Mn1', 'Mn2', 'Mn3', 'Mn4', 'Mn5', 'Mn6', 'Mn7', 'Mn8']

It is necessary to generate all possible combinations, taking 7 elements from the first list, and 1 from the second:
['Co8', 'Co2', 'Co3', 'Co4', 'Co5', 'Co6', 'Co7', 'Mn1']
['Co8', 'Co1', 'Co3', 'Co4', 'Co5', 'Co6', 'Co7', 'Mn2']
['Co8', 'Co1', 'Co2', 'Co4', 'Co5', 'Co6', 'Co7', 'Mn3']
['Co8, 'Co1', 'Co2', 'Co3', 'Co5', 'Co6', 'Co7', 'Mn4']
['Co8', 'Co1', 'Co2', 'Co3', 'Co4', 'Co6', 'Co7', 'Mn5']
['Co8', 'Co1', 'Co2', 'Co3', 'Co4', 'Co5', 'Co7', 'Mn6']
и т.д.

and then take 6 elements from the first list, and 2 from the second:
['Co8', 'Co2', 'Co3', 'Co4', 'Co5', 'Co6', 'Mn7', 'Mn1']
['Co1', 'Co7', 'Co8', 'Co4', 'Co5', 'Co6', 'Mn3', 'Mn2']
...
 и т.д.

And so you need to generate all combinations, reducing the number of elements from the first list and increasing from the second.

Combinations should not contain repetitions of elements with the same number (Co1 and Mn1, etc.):
['Co1, 'Co2', 'Co3', 'Co4', 'Co5', 'Co6', 'Mn1', 'Mn2']
['Co1', 'Co7', 'Co8', 'Co4', 'Co5', 'Co6', 'Mn7', 'Mn8']
...
 и т.д.


In the future, instead of Co1, Co2 ..., coordinates will be indicated in the xyz format (Example: 0.118167 0.376788 0.873409)

Tried like this:
comb1 = itertools.combinations(atoms, 6)
comb2 = itertools.combinations(dopedatoms, 2)
result = itertools.product(comb1, comb2)
but this results in duplicate elements.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hint000, 2021-05-01
@schokk64rus

With a clarification about the repetition of digits, this is equivalent to iterating over combinations of
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1
...
1 1 1 1 1 1 1 1
(total 256 combinations),
where 0 means that we take an element from atoms,
1 means that we take an element from dopedatoms, for example
1 0 0 0 0 0 0 0 corresponds to ['Mn1', 'Co2', 'Co3', 'Co4', 'Co5', 'Co6', 'Co7', 'Co8']
So you can loop from 0 to 255, split the loop variable into bits, and form combinations bit by bit.
The conditions do not mention the boundary cases 0 0 0 0 0 0 0 0 and 1 1 1 1 1 1 1 1, if they are not needed, then loop from 1 to 254.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question