Posts: 6
Threads: 2
Joined: Nov 2017
Nov-15-2017, 04:21 PM
(This post was last modified: Nov-15-2017, 04:23 PM by antoniomancera.)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.
Posts: 1,298
Threads: 38
Joined: Sep 2016
(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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 6
Threads: 2
Joined: Nov 2017
Nov-16-2017, 08:45 AM
(This post was last modified: Nov-16-2017, 08:48 AM by antoniomancera.)
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
1 2 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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
1 2 |
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
1 2 |
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.
|