Python Forum
Do something with all possible combinations of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do something with all possible combinations of a list
#1
Going to explain in simple language since googling this didn't really help me (unsure how to name this problem).

I have a list which items I want to check with eachother. Each combination triggers a function.
So a list with [1,2,3] should run a function for pair [1,2], for pair [1,3] and for pair [2,3]. Duplicates / themselves aren't needed [1,1] [2,1] [2,2] [3,1] [3,2]

My list might become huge (600K (if that is huge)) so I 'Yield' seems interesting too.

Below is my soup. Horrible, not logical, returns duplicates (I know Sick ).
listA = [1,2,3,4,5,6]
listB = listA[:]
output = []
for a in listA:	
	for b in listB:
		if b != a:
            //function here
			output.append([a,b])
print(output)
Reply
#2
(Sep-11-2019, 06:53 AM)3Pinter Wrote: Going to explain in simple language since googling this didn't really help me (unsure how to name this problem).

I have a list which items I want to check with eachother. Each combination triggers a function.
So a list with [1,2,3] should run a function for pair [1,2], for pair [1,3] and for pair [2,3]. Duplicates / themselves aren't needed [1,1] [2,1] [2,2] [3,1] [3,2]

My list might become huge (600K (if that is huge)) so I 'Yield' seems interesting too.

Below is my soup. Horrible, not logical, returns duplicates (I know Sick ).
listA = [1,2,3,4,5,6]
listB = listA[:]
output = []
for a in listA:	
	for b in listB:
		if b != a:
            //function here
			output.append([a,b])
print(output)

Hi!

I have just checked your program, and I think it does what you want (by the way, to comment in Python use # instead of // which would give an invalid syntax error):
listA = [1,2,3,4,5,6]
listB = listA[:]
output = []
for a in listA: 
    for b in listB:
        if b != a:
            # function here
            output.append([a,b])
print(output)
and that produces the following output:
Output:
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5]]
which leaves out [1, 1], [2,2], [3,3], [4,4], [5,5], [6,6].

I just noticed (I'm very sleepy now) that you consider duplicates also the inverse pairs of any other one, and you don't want them. Let me think about it...
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#3
It does leave out the 'themselves'-ones but not the duplicate matches.

In my case [1,6] will return the same result (if doing a function) as [6,1]
Reply
#4
(Sep-11-2019, 07:20 AM)3Pinter Wrote: It does leave out the 'themselves'-ones but not the duplicate matches.

In my case [1,6] will return the same result (if doing a function) as [6,1]

I think I found a way:
listA = [1,2,3,4,5,6]
listB = listA[:]
output = []
for a in listA: 
    for b in listB:
        if b != a and b > a:
            # function here
            output.append([a,b])
print(output)
and that little modification produces the following output:
Output:
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6], [4, 5], [4, 6], [5, 6]]
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#5
foo =  [1, 2, 3, 4, 5, 6]

for idx, num1 in enumerate(foo[:-1:], start=1):
    for num2 in foo[idx:]:
        print(num1, num2)

print( '=' * 10)
# better use itertools.combinations
import itertools
for num1, num2 in itertools.combinations(foo, 2):
    print(num1, num2)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Ah look at that. Nice.

Now the 600K items in a list part.
I'm by no means a programmer and do this purely because I'm an enthousiast. So I can 'see' potential inefficient parts / runtime issues. Could you guys share your thoughts on it:

1. I'm creating a duplicate list of listA to check items.
Back to the 600K list ... that would be 2x 600K list.
To my own logic I would use first item in list comparing it with the rest of the list, then the second etc.

2. Would Yield be of any help?


# Sorry Buran, I saw your post of posting my own reply. I will look at yours now
Reply
#7
(Sep-11-2019, 07:35 AM)3Pinter Wrote: Ah look at that. Nice.

Now the 600K items in a list part.
I'm by no means a programmer and do this purely because I'm an enthousiast. So I can 'see' potential inefficient parts / runtime issues. Could you guys share your thoughts on it:

1. I'm creating a duplicate list of listA to check items.
Back to the 600K list ... that would be 2x 600K list.
To my own logic I would use first item in list comparing it with the rest of the list, then the second etc.

2. Would Yield be of any help?
I'm just a newbie, so my lack of knowledge is huge!!!
I just try to help if I think I can, or if I think it would be a nice challenge, even with my current level of knowledge.

Many posters here are much much more knowledgeable than I am, so while they use neater and more efficient ways of writing Python, I just write code in a much closer way to natural language which for me, it's easier to understand by myself, at my current level of knowledge.

I hope later on, to be able to write neater and better code...

Therefore, about your second question, I have no idea of what Yield is, not even itertools.

About your first question, if you follow the program you originally posted, it seems that you
are taking first, the second list (b's, listB, inner loop), and from that second list, you take the first, then the second... and compare it with the elements of the first list (a's, listA, outer loop), again, one by one. So, being the same size the two lists, I don't think it would produce any problem, either way.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
(Sep-11-2019, 07:35 AM)3Pinter Wrote: So I can 'see' potential inefficient parts / runtime issues. Could you guys share your thoughts on it:
# Sorry Buran, I saw your post of posting my own reply. I will look at yours now

itertools is as effective you can get in Python:

Quote:The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 837 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  How can I find all combinations with a regular expression? AlekseyPython 0 1,637 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  All possible combinations CODEP 2 1,826 Dec-01-2020, 06:10 PM
Last Post: deanhystad
  Triplet Combinations of All Values quest 2 1,938 Nov-05-2020, 09:22 AM
Last Post: quest
  All possible combinations of multiplications Shreya10o 0 1,632 May-23-2020, 07:45 AM
Last Post: Shreya10o
  counting items in a list of number combinations Dixon 2 2,030 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Python program that list all possible football outcome combinations lukorir 5 8,776 Oct-17-2019, 04:05 AM
Last Post: steve_shambles
  list of string combinations Skaperen 8 3,245 May-22-2019, 01:18 PM
Last Post: Skaperen
  Python to iterate a number of possible combinations teflon 4 3,886 Apr-24-2019, 03:00 AM
Last Post: scidam
  Distances between all combinations in datatset amyd 6 3,802 Dec-13-2018, 01:23 PM
Last Post: amyd

Forum Jump:

User Panel Messages

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