0

I'm trying to make a loop that finds distances of values of one list, to the values of another list.

The data itself is of varying dimensions in a coordinates layout. Here is an example

x = ['1.23568 1.589887', '1.989 1.689']
y = ['2.5689 1.5789', '2.898 2.656']

I would like to be able to make a separate list for each y value and its distance from each x value. There are always more x values than y values.

This is what I have so far:

def distances_y(x,y):
    for i in y:
        ix = [i.split(' ',)[0] for i in y]
        for z in x:
            zx = [z.split('',1)[0] for z in x]
            distances_1 = [zx - ix for z in x]
            return distances_1
        print(i +"_"+"list") = [distance_1]

But I'm stuck on how to create individual lists for each y value. Each distance also needs to be a list in itself, a list in a list so to speak.

The largest problem is that I am unable to use packages besides tkinter for this.

1
  • Pro-tip: When processing 2+ lists like this, you can use i, j, k for the loop- counter variable of the x,y,z lists. Commented Sep 12, 2021 at 2:40

2 Answers 2

1

Try using a dictionary instead:

def distances_y(x,y):
    dct = {}
    for i in y:
        ix = [i.split(' ',)[0] for i in y]
        for z in x:
            zx = [z.split('',1)[0] for z in x]
            distances_1 = [zx - ix for z in x]
            return distances_1
        dct[i +"_"+"list"] = [distance_1]

And to get the values, do:

print(dct)

And if you want to get a specific key name, try:

print(dct[<key name over here>])
Sign up to request clarification or add additional context in comments.

Comments

0

And if you want a 2-d array:

for each x you would add

my2d.append([])

and for each y

my2d[i].append(x[i] - y[j])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.