Python Forum

Full Version: List of far points of two list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I'm trying to create a program that for two list xs and ys, return a list zs with only the element of ys that their distance of any element of xs be at least d.
def distance(a,b):
    y=sqrt((a.real-b.real)**2+(a.imag-b.imag)**2)
    return y


    

def dist(xs,ys,d):
    zs=[]
    for i in range(0,len(xs)-1):
        for k in range(0,len(ys)-1):
            if distancia(xs[i],ys[k])<=d:
                zs=zs 
                break
            else:
                zs=zs+[ys[i]]
    return zs
But it's wrong, and I don't know how to fixed it. Could you help me please?.

Thank you.
(Nov-15-2017, 04:21 PM)antoniomancera Wrote: [ -> ]But it's wrong,

Is not helpful, if you are getting an error, post the error code in it's entirety. What output are you getting versus what you expect.  Include a small sample of 'xs', 'ys' and 'd'.

Python strongly encourages the use of descriptive variables, so stop using meaningless single and short variable names.
I'm sorry, I wanted to say that the output aren't that I expected. Because for example if I input xs=[1,2], ys=[3,4,5], and d=0.5. The output it's
dist([1,2],[3,4,5],1)
Out[7]: [3, 3]
. However, if the program was well wrote should output [4,5].
I have created this new program
def distance(a,b):
    y=sqrt((a.real-b.real)**2+(a.imag-b.imag)**2)
    return y

def distancelist(list1,a):
    y=[]
    for i in range(0,len(list1)):
        y=y+[distance(list1[i],a)]
    return y
    



        
def countthefarpoint(list1,a,dis):
    y=distancelist(list1,a)
    cont=0
    for i in range(0,len(y)-1):
        if y[i]>dist:
            cont=cont+1
        else:
            cont=cont
    return cont
The program distance(a,b) output the distance between a and b. The program distancelist output a list with the distnace between a and each element of list1, for example for a=2 lis1=[2,3,4,7,-1j] the ouput it's
distancelist([2,3,4,7,-1j],2)
Out[10]: [0.0, 1.0, 2.0, 5.0, 2.2360679774997898]
. And the program countthefarpoint take a list "list" a number "a" and a distance "dist", and output how many times the distance between "a" and each element of "list1" it's bigger than "dist". For a=2 list1=[2,3,4,7,-1j], and dist=2, should output 3, but the output it's
countthefarpoint([2,3,4,7,-1j],2,1)
Out[12]: 0
. I have created the program countthefarpoint, beacause I want to create a program which for two list, lis1 and list2,and a distance dist. Output list1, and the elements of list2 which their distance of each element of list1 be at least dist.