Python Forum

Full Version: Python show the combinations of list of elements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
letters = [ 'AB', 'CD', 'EF', 'GH']
I have a list of 4 elements with 2-letter each element, I want to find the combinations for the following:
C4_2 = [ 'ABCD', 'ABEF', 'ABGH', 'CDEF', 'CDGH', 'EFGH' ]
C4_3 = [ 'ABCDEF', 'ABCDGH', 'ABEFGH', 'CDEFGH' ]
c4_4 = [ 'ABCDEFGH' ]
I can’t find any example for this, the issue is: each element of 2 letters is treated as a single unit.
Please show me your code.
I know I have to use itertools, but I can’t figure this out yet.
Thanks,
You never HAVE to use itertools. It's often just a more efficient way of doing things. If you loop through the items, then use the index to loop through the remaining elements, you can build pairs easily:
>>> items = ['AB', 'CD', 'EF', 'GH']
>>> combined = []
>>> for ndx, item in enumerate(items):
...   if len(items) > ndx+1:
...     current = items[ndx]
...     for next_item in range(ndx+1, len(items)):
...       combined.append(current + items[next_item])
...
>>> combined
['ABCD', 'ABEF', 'ABGH', 'CDEF', 'CDGH', 'EFGH']
Hello:
Thank you very much, your code works for C4_2:
C4_2 = [ 'ABCD', 'ABEF', 'ABGH', 'CDEF', 'CDGH', 'EFGH' ]
But in my original post, I need other 2 combination results:
C4_3 = [ 'ABCDEF', 'ABCDGH', 'ABEFGH', 'CDEFGH' ]
c4_4 = [ 'ABCDEFGH' ]
I want to know if I can make your code as a function, and pass a parameter as the number I want to pick, like 3 or 4.
But I am rather new for python programming, I can't figure this out.
Please advice.
Thanks,
(Mar-06-2018, 09:01 PM)zydjohn Wrote: [ -> ]Please advice.
Please, do yourself a service, and never use that phrase again. It's nonsense words, and it's continued usage baffles me.

(Mar-06-2018, 09:01 PM)zydjohn Wrote: [ -> ]I want to know if I can make your code as a function, and pass a parameter as the number I want to pick, like 3 or 4.
Yes, you can. If your attempt isn't working, show us what you're trying so we can help.
Hello:
I have figured out the following:
def combines(items, picks, len_unit):
    combined = []
    for ndx, item in enumerate(items):
        current = items[ndx]
        remains = [n for n in items if n != current]
        for ndy, other in enumerate(remains):
            current = current + remains[ndy]
            if len(current) == (picks * len_unit):
               combined.append(current)
    return(combined)

letters = ['AB', 'CD', 'EF', 'GH']
C4_3 = combines(letters, 3, 2)
print(C4_3)
C4_4 = combines(letters, 4, 2)
print(C4_4)
Looks like C4_3 is correct, but for C4_4, I can see its value:
C4_4 = ['ABCDEFGH', 'CDABEFGH', 'EFABCDGH', 'GHABCDEF']
But for my specific question, all the elements in C4_4 are the same, as in my original post, the order of letters in the combination does NOT matter, therefore: 'ABCDEFGH' == 'CDABEFGH' == 'EFABCDGH' == 'GHABCDEF'.
So, I want to add some condition to return only the first element, not all 4 elements for C4_4.
C4_4 = ['ABCDEFGH']
Any suggestions?