Damo H
Damo H

Reputation: 77

creating multible variable lists in a loop in python

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.

Upvotes: 0

Views: 37

Answers (2)

Adam Smooch
Adam Smooch

Reputation: 1322

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])

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71620

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>])

Upvotes: 1

Related Questions