Hi, I wonder what the best way to compare one value in a tuple/list with all other, and not itself.

I'm taking my values from a file and thos file can be very big so I also wonder how to optimize this, not to much so you can't see what it does.

for the moment I use something like this:

f = file(filename)
    cell2 = 0
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        
        #do something...
        
        cell2 = cell2+1
    f.close()

... but I don't think this is a good solution (?). This don't jump over the value itself, and if the static value is the last value in the tuple/list it do not 'restart' from the beginning..

I hope you understand what I want /flaerpen

hi, I would like to give some tips, the code you have posted is not indented properly. Which does not at all show what you have tried so far.

I'm taking my values from a file and thos file can be very big so I also wonder how to optimize this, not to much so you can't see what it does.

Are you reading the values from a ordinary text file? Because I dont understand cell2=0 Ok, if you want to compare an element against reading the values from a *text* file, line-by-line, create a temp list or tuple from the line. for eg:

fsock = open('searchFile.txt','r')
for line in fsock.readline():
    li = line.split()# for eg: delimeter is ' '
    if searchElement in li:
        print searchElement,' found'
fsock.close()

If you want to compare a *list of element* against the file, i prefer you create a list or tuple of values from the file. Then loop through the list of search element and use in operator to find the presence of element. Ofcourse if the search list is much bigger than the element list from file then loop through the file to find the element.

And if the file tooooo big then you have always have an option to read the specified number of bytes from the file.

kath.

the code you have posted is not indented properly.

yes, I saw that now.. It's not like that in the code.. And it is also a ordinary file like this (x,y,r are numbers):

type,x,y,r
type,x,y,r
type,x,y,r
...

I dont understand cell2=0

well, I think it's a old habit from C++, can't explain it really :P.

fsock = open('searchFile.txt','r')
for line in fsock.readline():
    li = line.split()# for eg: delimeter is ' '
    if searchElement in li:
        print searchElement,' found'
fsock.close()

Okej, shouldn't it be 'fsock.readlines()'? For the moment it reads all the lines, which is good but if it comes a blank line it shouldn't read that --> not good because my files always ends with a blank line.

I'm also not sure how to use your code. Maybe it's because I explains bad..

I don't know what more to say. I solved it but I'm not happy.. I think I'll post what I have done and maybe someone could tell me what to do to improve it.. SEE MY POST BELOW

from math import sqrt
import cells

def checkAllCells(filename,cell):
    x0 = int(cells.getXpos(filename,cell)) #returns the X-coordinate for that specific cell (line)
    y0 = int(cells.getYpos(filename,cell)) #returns the Y-coordinate for that specific cell (line)
    r0 = int(cells.getRadius(filename, cell)) #returns the radian for that specific cell (line)
    
    f = file(filename)
    intersects = [] #creates a tuple/list where I'll put all of the solution that is right
    cell2 = 0 #I don't know really, shall I skip this line?
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        
        dx = int(cells.getXpos(filename, cell2)) - int(x0)
        dy = int(cells.getYpos(filename, cell2)) - int(y0)
        r1 = int(cells.getRadius(filename, cell2))
        d = sqrt((dy*dy) + (dx*dx))
        
        if (d > (r0 + r1)) or (d < abs(r0 - r1)) or (cell == cell2):
#            no solution. circles do not intersect.
            print 'No solution'
        else:
#            There is a solution.
            intersects.append(cell2) #add a cell that is correct
        
        cell2 = cell2+1
        
    f.close()
    return intersects

Hi,

d = sqrt((dy*dy) + (dx*dx))

instead of importing sqrt from math, you can do this for the same result

d = ((dy*dy) + (dx*dx)) ** 0.5 # it will be fast.

could you please tell what is the type of cell in checkAllCells(filename,cell) as 'cell'. Because i cannot figure out from the code > if (d > (r0 + r1)) or (d < abs(r0 - r1)) or (cell == cell2): Because cell2 is integer(from the code, which you have posted). If cell is integer then meaning of cell is wrong, from what you have said earlier, cell(line). BTW, where is tuple/list comparission?

>>> import cells
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    import cells
ImportError: No module named cells
>>>

??

Hi,

d = ((dy*dy) + (dx*dx)) ** 0.5 # it will be fast.

Great!

A cell 'contains' (type,x,y,r) and one cell occur only one time per line, therefore you can say that cell(5) is on line 4.

>>> import cells
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    import cells
ImportError: No module named cells
>>>

'cells' is a module I have written (cells.py)

I can also say that this is my first "big" program that I make..

Thank you for your time! /flaerpen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.