Interacting all items on a list

Asked

Viewed 36 times

0

I have the following situation:

I have 4 numerical lists ranging from 1 to 30. Ex.:

AP_X = [1,2,3,4,5...30]
AP_Y = [1,2,3,4,5...30]
demanda_X = [1,2,3,4,5...30]
demanda_Y = [1,2,3,4,5...30]
  • I have another list, empty, called distancia that will receive the result of these interactions.

    distancia = []
    
  • The lists AP_X and AP_Y represent the coordinates of a point and the lists demandas_X and demanda_Y, represent the coordinates of another point.

  • I need to make every point AP(x,y) match with each point demanda(x,y).

  • For example:

    distancia = [1,1,1,1 | 1,1,2,2 | 1,1,3,3 |...| 2,2,1,1 | 2,2,2,2| ...| 30,30,1,1 | 30,30,2,2 ]
    

I don’t know if I made myself clear, but I need to match every item AP(x,y) with all demanda(x,y).

Thanks in advance for your help.

  • In fact I couldn’t understand what kind of combination you need. Can you explain better?

  • For example: Say I have a list with [ a,b,c] and another list with [e,f,g]. I need the result to be [ae,af,ag,be,bf,bg,ce,cf,cg].

  • However, in my example the abcdefg = xy coordinates,

  • Excuse me if I’m not clear. I’m trying hard.

1 answer

0


AP_X = [1,2,3,4,5,30]
AP_Y = [1,2,3,4,5,30]
demanda_X = [1,2,3,4,5,30]
demanda_Y = [1,2,3,4,5,30]

ap = list(zip(AP_X, AP_Y))
demanda = list(zip(demanda_X, demanda_Y))

distancia = []

for i in ap:
  for j in demanda:
    distancia.append((i, j))

print(distancia)

This code prints the following result:

[((1, 1), (1, 1)), ((1, 1), (2, 2)), ((1, 1), (3, 3)), ((1, 1), (4, 4)), ((1, 1), (5, 5)), ((1, 1), (30, 30)), ((2, 2), (1, 1)), ((2, 2), (2, 2)), ((2, 2), (3, 3)), ((2, 2), (4, 4)), ((2, 2), (5, 5)), ((2, 2), (30, 30)), ((3, 3), (1, 1)), ((3, 3), (2, 2)), ((3, 3), (3, 3)), ((3, 3), (4, 4)), ((3, 3), (5, 5)), ((3, 3), (30, 30)), ((4, 4), (1, 1)), ((4, 4), (2, 2)), ((4, 4), (3, 3)), ((4, 4), (4, 4)), ((4, 4), (5, 5)), ((4, 4), (30, 30)), ((5, 5), (1, 1)), ((5, 5), (2, 2)), ((5, 5), (3, 3)), ((5, 5), (4, 4)), ((5, 5), (5, 5)), ((5, 5), (30, 30)), ((30, 30), (1, 1)), ((30, 30), (2, 2)), ((30, 30), (3, 3)), ((30, 30), (4, 4)), ((30, 30), (5, 5)), ((30, 30), (30, 30))]

Where each item is of the format ((AP_X, AP_Y), (demanda_X, demanda_Y))

It suits you or needs to be in the format AP_X, AP_Y, demanda_X, demanda_Y?

  • Perfect, Anderson! That’s exactly how I need it. Thank you very, very much. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.