Python Forum

Full Version: Do something with all possible combinations of a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
(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...
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]
(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,
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)
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
(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,
(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.