Python show the combinations of list of elements - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python show the combinations of list of elements (/thread-8763.html) |
Python show the combinations of list of elements - zydjohn - Mar-06-2018 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, RE: Python show the combinations of list of elements - micseydel - Mar-06-2018 Take a look at https://docs.python.org/3/library/itertools.html#itertools.combinations RE: Python show the combinations of list of elements - buran - Mar-06-2018 check itertools.combinations RE: Python show the combinations of list of elements - nilamo - Mar-06-2018 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'] RE: Python show the combinations of list of elements - zydjohn - Mar-06-2018 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, RE: Python show the combinations of list of elements - nilamo - Mar-06-2018 (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. RE: Python show the combinations of list of elements - zydjohn - Mar-06-2018 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? |