Posts: 32
Threads: 19
Joined: Dec 2017
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,
Posts: 2,342
Threads: 62
Joined: Sep 2016
Posts: 8,170
Threads: 160
Joined: Sep 2016
Posts: 3,458
Threads: 101
Joined: Sep 2016
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']
Posts: 32
Threads: 19
Joined: Dec 2017
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,
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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.
Posts: 32
Threads: 19
Joined: Dec 2017
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?
|