Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A big for in for Loop!
#1
Hello guys
good day
I have a list of near 10000 points with x,y,z coordinates and a fourth property which is 1 or 2 or 3 (let call it p).
The points are mostly in pair, I mean for almost 95% of points, there is another point with same x,y,z and p.
I need to find out for a point if there is any other point with same x,y,z,p or not!

I have tried a for in for loop in python , something like:
for i=1 to 10000
for j=1 to 10000
if x(i)=x(j) and y(i)=y(j) and z(i)=z(j) and p(i)=p(j)
then put a comment

the code is working well if i have small number of points. for example for a number of point of 500 it takes 10 minutes for python to run.
The problem is that when i put n=10000, it takes a lot of time (near 4 hours)and my computer falls down without any answer.

Would you kindly please show me a solution?
Thanks in advance
Reply
#2
what object is used to represent a point - tuple/named tuple, custom class?
this line if x(i)=x(j) and y(i)=y(j) and z(i)=z(j) and p(i)=p(j) suggest you have separate collections for each attribute, but using () to access elements would not work. unless this is pseudo-code.
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
#3
(Sep-03-2020, 06:10 AM)buran Wrote: what object is used to represent a point - tuple/named tuple, custom class?
this line if x(i)=x(j) and y(i)=y(j) and z(i)=z(j) and p(i)=p(j) suggest you have separate collections for each attribute, but using () to access elements would not work. unless this is pseudo-code.

Hello

i have x,y,z and P in separated columns,,,i have one list for x, one for y , ...
i am looking for a new programming logic or simplify my for in for loop, because for 10000 points it should compare 10000 x 10000 states and it rakes a lot of time with no answer at the end!
Reply
#4
can you show us some actual code with sample input and expected output - i.e. is this csv file, do you work with pandas, etc.?
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
#5
10000 isn't a large number for modern machines. Use a counter to count all the points. Then print all the points that have a count greater than 1.

from collections import Counter
all_points = Counter(zip(x,y,z,p))
duplicate_points = [i for i in all_points if all_points[i] > 1]
Reply
#6
(Sep-03-2020, 06:30 AM)bowlofred Wrote: 10000 isn't a large number for modern machines. Use a counter to count all the points. Then print all the points that have a count greater than 1.

from collections import Counter
all_points = Counter(zip(x,y,z,p))
duplicate_points = [i for i in all_points if all_points[i] > 1]

Thank you. It seems to be much better.
I also modified my for loops, because i had some IF conditions inside the for loop.
by putting them out the problem solved.
Thanks again for your help!
Reply


Forum Jump:

User Panel Messages

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