Python Forum
List of far points of two list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of far points of two list
#1

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.
Reply
#2
(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
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,267 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
Question Python - List - Int List sophi 8 2,519 Apr-21-2022, 07:55 PM
Last Post: sophi
  Check if a list exists in given list of lists Daniel94 2 2,227 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  list of strings to list of float undoredo 3 2,668 Feb-19-2020, 08:51 AM
Last Post: undoredo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,142 Oct-20-2019, 06:24 PM
Last Post: Larz60+
  have homework to add a list to a list using append. celtickodiak 2 1,991 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Search character from 2d list to 2d list AHK2019 3 2,461 Sep-25-2019, 08:14 PM
Last Post: ichabod801
  I donr know how to search from 2d list to 2d list AHK2019 1 1,748 Sep-25-2019, 01:59 AM
Last Post: ichabod801
  sort a list alphabeticaly without changing the original list Holmen 5 4,188 Jan-27-2019, 01:49 PM
Last Post: Holmen
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,569 Oct-24-2018, 06:40 PM
Last Post: LeSchakal

Forum Jump:

User Panel Messages

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