Posts: 67
Threads: 25
Joined: Jun 2018
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  ).
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)
Posts: 212
Threads: 25
Joined: Aug 2019
Sep-11-2019, 07:15 AM
(This post was last modified: Sep-11-2019, 07:21 AM by newbieAuggie2019.)
(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 ).
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
Posts: 67
Threads: 25
Joined: Jun 2018
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]
Posts: 212
Threads: 25
Joined: Aug 2019
(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
Posts: 8,160
Threads: 160
Joined: Sep 2016
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)
Posts: 67
Threads: 25
Joined: Jun 2018
Sep-11-2019, 07:35 AM
(This post was last modified: Sep-11-2019, 07:36 AM by 3Pinter.)
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
Posts: 212
Threads: 25
Joined: Aug 2019
(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
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
|