Search code examples
pythoncombinationspymatgen

Getting all combinations of a string with subscript and specif format


I have seen various combination formats. But I have to try something new. Lets say, we have three lists:

A = ('Li', 'B', 'C', 'N')
B = ('Li', 'B', 'C', 'N')  
X = ('O', 'Br', 'Cl')

All the element of A are same as B. Now I need to create all possible combinations in the following formats:

# (i)   ABX3
# (ii)  AA'B2X6
# (iii) A2B2X6

Where A2 (is A_{2}) is 'A' with 2 in subscript and A!=A' in the combination.

Here is what I have tried so far:

import itertools
elem_list = ['Li','B','C','N']
CCA_combos1 = list(itertools.combinations(elem_list, 2)) 
display(CCA_combos1, len(CCA_combos1))
   
listX1 = ('O','Cl', 'Br')
CCA_combosX = []
for x in range(len(CCA_combos1)):
  for y in range(0,len(listX1)):
    CCA_combosX += [tuple(CCA_combos1[x]) + tuple(listX1[y])]

resX1 = [''.join(tups) for tups in CCA_combosX]

The above code not working well for Br and Cl I have no idea yet to create combination in A2BB'X6.


Solution

  • You could use itertools.product. I hope I understood your question correctly.

    from itertools import product
    
    A = ('Li', 'B', 'C', 'N')
    B = ('Li', 'B', 'C', 'N')
    X = ('O', 'Br', 'Cl')
    
    list1, list2, list3 = [], [], []
    
    # (i)   ABX3
    for a, b, x in product(A, B, X):
        print(a, b, x, 3)
        list1.append((a, b, x, 3))
    
    # (ii)  AA'B2X6
    for a, ap, b, x in product(A, A, B, X):
        if a != ap:
            print(a, ap, b, 2, x, 6)
            list2.append((a, ap, b, 2, x, 6))
    
    # (iii) A2B2X6
    for a, b, x in product(A, B, X):
        print(a, 2, b, 2, x, 6)
        list3.append((a, 2, b, 2, x, 6))
        
        
    # print the number of elements in each list
    print('\n(i)   ABX3:', len(list1))
    print('(ii)  AA\'B2X6:', len(list2))
    print('(iii) A2B2X6:', len(list3))
    

    Outputs:

    # (i)
    Li Li O 3
    Li Li Br 3
    Li Li Cl 3
    Li B O 3
    Li B Br 3
    Li B Cl 3
    Li C O 3
    Li C Br 3
    Li C Cl 3
    Li N O 3
    Li N Br 3
    Li N Cl 3
    B Li O 3
    B Li Br 3
    B Li Cl 3
    B B O 3
    B B Br 3
    B B Cl 3
    B C O 3
    B C Br 3
    B C Cl 3
    B N O 3
    B N Br 3
    B N Cl 3
    C Li O 3
    C Li Br 3
    C Li Cl 3
    C B O 3
    C B Br 3
    C B Cl 3
    C C O 3
    C C Br 3
    C C Cl 3
    C N O 3
    C N Br 3
    C N Cl 3
    N Li O 3
    N Li Br 3
    N Li Cl 3
    N B O 3
    N B Br 3
    N B Cl 3
    N C O 3
    N C Br 3
    N C Cl 3
    N N O 3
    N N Br 3
    N N Cl 3
    
    # (ii)
    Li B Li 2 O 6
    Li B Li 2 Br 6
    Li B Li 2 Cl 6
    Li B B 2 O 6
    Li B B 2 Br 6
    Li B B 2 Cl 6
    Li B C 2 O 6
    Li B C 2 Br 6
    Li B C 2 Cl 6
    Li B N 2 O 6
    Li B N 2 Br 6
    Li B N 2 Cl 6
    Li C Li 2 O 6
    Li C Li 2 Br 6
    Li C Li 2 Cl 6
    Li C B 2 O 6
    Li C B 2 Br 6
    Li C B 2 Cl 6
    Li C C 2 O 6
    Li C C 2 Br 6
    Li C C 2 Cl 6
    Li C N 2 O 6
    Li C N 2 Br 6
    Li C N 2 Cl 6
    Li N Li 2 O 6
    Li N Li 2 Br 6
    Li N Li 2 Cl 6
    Li N B 2 O 6
    Li N B 2 Br 6
    Li N B 2 Cl 6
    Li N C 2 O 6
    Li N C 2 Br 6
    Li N C 2 Cl 6
    Li N N 2 O 6
    Li N N 2 Br 6
    Li N N 2 Cl 6
    B Li Li 2 O 6
    B Li Li 2 Br 6
    B Li Li 2 Cl 6
    B Li B 2 O 6
    B Li B 2 Br 6
    B Li B 2 Cl 6
    B Li C 2 O 6
    B Li C 2 Br 6
    B Li C 2 Cl 6
    B Li N 2 O 6
    B Li N 2 Br 6
    B Li N 2 Cl 6
    B C Li 2 O 6
    B C Li 2 Br 6
    B C Li 2 Cl 6
    B C B 2 O 6
    B C B 2 Br 6
    B C B 2 Cl 6
    B C C 2 O 6
    B C C 2 Br 6
    B C C 2 Cl 6
    B C N 2 O 6
    B C N 2 Br 6
    B C N 2 Cl 6
    B N Li 2 O 6
    B N Li 2 Br 6
    B N Li 2 Cl 6
    B N B 2 O 6
    B N B 2 Br 6
    B N B 2 Cl 6
    B N C 2 O 6
    B N C 2 Br 6
    B N C 2 Cl 6
    B N N 2 O 6
    B N N 2 Br 6
    B N N 2 Cl 6
    C Li Li 2 O 6
    C Li Li 2 Br 6
    C Li Li 2 Cl 6
    C Li B 2 O 6
    C Li B 2 Br 6
    C Li B 2 Cl 6
    C Li C 2 O 6
    C Li C 2 Br 6
    C Li C 2 Cl 6
    C Li N 2 O 6
    C Li N 2 Br 6
    C Li N 2 Cl 6
    C B Li 2 O 6
    C B Li 2 Br 6
    C B Li 2 Cl 6
    C B B 2 O 6
    C B B 2 Br 6
    C B B 2 Cl 6
    C B C 2 O 6
    C B C 2 Br 6
    C B C 2 Cl 6
    C B N 2 O 6
    C B N 2 Br 6
    C B N 2 Cl 6
    C N Li 2 O 6
    C N Li 2 Br 6
    C N Li 2 Cl 6
    C N B 2 O 6
    C N B 2 Br 6
    C N B 2 Cl 6
    C N C 2 O 6
    C N C 2 Br 6
    C N C 2 Cl 6
    C N N 2 O 6
    C N N 2 Br 6
    C N N 2 Cl 6
    N Li Li 2 O 6
    N Li Li 2 Br 6
    N Li Li 2 Cl 6
    N Li B 2 O 6
    N Li B 2 Br 6
    N Li B 2 Cl 6
    N Li C 2 O 6
    N Li C 2 Br 6
    N Li C 2 Cl 6
    N Li N 2 O 6
    N Li N 2 Br 6
    N Li N 2 Cl 6
    N B Li 2 O 6
    N B Li 2 Br 6
    N B Li 2 Cl 6
    N B B 2 O 6
    N B B 2 Br 6
    N B B 2 Cl 6
    N B C 2 O 6
    N B C 2 Br 6
    N B C 2 Cl 6
    N B N 2 O 6
    N B N 2 Br 6
    N B N 2 Cl 6
    N C Li 2 O 6
    N C Li 2 Br 6
    N C Li 2 Cl 6
    N C B 2 O 6
    N C B 2 Br 6
    N C B 2 Cl 6
    N C C 2 O 6
    N C C 2 Br 6
    N C C 2 Cl 6
    N C N 2 O 6
    N C N 2 Br 6
    N C N 2 Cl 6
    
    # (iii)
    Li 2 Li 2 O 6
    Li 2 Li 2 Br 6
    Li 2 Li 2 Cl 6
    Li 2 B 2 O 6
    Li 2 B 2 Br 6
    Li 2 B 2 Cl 6
    Li 2 C 2 O 6
    Li 2 C 2 Br 6
    Li 2 C 2 Cl 6
    Li 2 N 2 O 6
    Li 2 N 2 Br 6
    Li 2 N 2 Cl 6
    B 2 Li 2 O 6
    B 2 Li 2 Br 6
    B 2 Li 2 Cl 6
    B 2 B 2 O 6
    B 2 B 2 Br 6
    B 2 B 2 Cl 6
    B 2 C 2 O 6
    B 2 C 2 Br 6
    B 2 C 2 Cl 6
    B 2 N 2 O 6
    B 2 N 2 Br 6
    B 2 N 2 Cl 6
    C 2 Li 2 O 6
    C 2 Li 2 Br 6
    C 2 Li 2 Cl 6
    C 2 B 2 O 6
    C 2 B 2 Br 6
    C 2 B 2 Cl 6
    C 2 C 2 O 6
    C 2 C 2 Br 6
    C 2 C 2 Cl 6
    C 2 N 2 O 6
    C 2 N 2 Br 6
    C 2 N 2 Cl 6
    N 2 Li 2 O 6
    N 2 Li 2 Br 6
    N 2 Li 2 Cl 6
    N 2 B 2 O 6
    N 2 B 2 Br 6
    N 2 B 2 Cl 6
    N 2 C 2 O 6
    N 2 C 2 Br 6
    N 2 C 2 Cl 6
    N 2 N 2 O 6
    N 2 N 2 Br 6
    N 2 N 2 Cl 6
    
    (i)   ABX3: 48
    (ii)  AA'B2X6: 144
    (iii) A2B2X6: 48