Python Forum
Python show the combinations of list of elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python show the combinations of list of elements
#1
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,
Reply
#2
Take a look at https://docs.python.org/3/library/iterto...mbinations
Reply
#3
check itertools.combinations
Reply
#4
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']
Reply
#5
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,
Reply
#6
(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.
Reply
#7
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 986 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,023 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  Finding combinations of list of items (30 or so) LynnS 1 838 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  PIL Image im.show() no show! Pedroski55 2 928 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  How to change the datatype of list elements? mHosseinDS86 9 1,911 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  python basemap, cant show the right coordinates dbsr 0 943 Jun-08-2022, 01:55 PM
Last Post: dbsr
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  PIL Image im.show() no show! Pedroski55 6 4,740 Feb-08-2022, 06:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020