Python Forum
A big for in for Loop! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: A big for in for Loop! (/thread-29454.html)



A big for in for Loop! - faryad13 - Sep-03-2020

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


RE: A big for in for Loop! - buran - Sep-03-2020

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.


RE: A big for in for Loop! - faryad13 - Sep-03-2020

(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!


RE: A big for in for Loop! - buran - Sep-03-2020

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.?


RE: A big for in for Loop! - bowlofred - Sep-03-2020

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]



RE: A big for in for Loop! - faryad13 - Sep-03-2020

(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!