1

I need to combine values in two lists(longitude/latitude), to get one combined list (not in tuple or string format) My simple for loop is not iterating correctly over the first list, it is using one value to match all values in the second list, before moving to the next value.

for a in location1:
    for b in location2:
        location3.append(a+b)
location3

I am getting this (one 'a' matching to all 'b's first) :

[[41.7770923949, -87.6060037796],
[41.7770923949, -87.6547753762],
[41.7770923949, -87.5716351762],

I want this matching the same sequence in both lists:

[41.7770923949, -87.6060037796],
[41.784575915, -87.6547753762],

Also, trying things with zip/map like this:

list(map(lambda X: (X[0],X[1]), list(zip(location1,location2))))

Gives me this not in the correct form (is there another way?):

[([41.7770923949], [-87.6060037796]),
([41.784575915], [-87.6547753762]),
Gazoo
  • 37
  • 6

3 Answers3

1

You need to use zip directly to iterate over the two lists, not with your lambda.

l1 = [ 2, 4, 6, 8 ]
l2 = [ 1, 3, 5, 7 ]

coord = []

for a, b in zip( l1, l2 ):
    coord.append( [ a, b ] )

print( coord )

By the way, your question is related to this one.

Camille
  • 56
  • 3
  • right ,but still the zip gives this format: `[[[41.7770923949], [-87.6060037796]], [[41.784575915], [-87.6547753762]], [[41.7442798961], [-87.5716351762]], ` – Gazoo Nov 19 '21 at 08:01
  • Because your input is a list of lists of 1 element, like `[[41.7770923949], [41.784575915],...]`? You should have given the input lists so we could know it ;) then you can use `coord.append( [ a[0], b[0] ] )` instead. – Camille Nov 19 '21 at 09:23
  • Sorry. I posted the image, not sure where it went. thanks for explaining. – Gazoo Nov 19 '21 at 12:39
0

You don't need the second for in your code. I think your data structure is like this:

location1 = [[1], [2], [3]]
location2 = [[4], [5], [6]]

Then try this:

location3 = []
for i, _ in enumerate(location1):
    location3.append(location1[i]+location2[i])
print(location3)

Result:

[[1, 4], [2, 5], [3, 6]]
Soroosh Noorzad
  • 503
  • 1
  • 6
  • 18
  • yes, correct output: `[[41.7770923949, -87.6060037796], [41.784575915, -87.6547753762], [41.7442798961, -87.5716351762],` – Gazoo Nov 19 '21 at 08:04
  • @Gazoo Was my answer wrong? If not, I will appreciate a vote up. It's good to vote up the answers which lead to the correct solution. TNX – Soroosh Noorzad Nov 19 '21 at 10:46
  • 1
    I did, but too new so it didn't do anything. – Gazoo Nov 19 '21 at 12:09
0

You need to show both variables location1 and location 2. Final solution will be depends on it.

case 1: Both variables are list of list then below solution is for you.

location1 = [[1], [2], [3]]
location2 = [[4], [5], [6]]
location3 = list(map(lambda coord: [coord[0][0],coord[1][0]], zip(location1,location2)))
print(location3) 

case 2: both variables list of integers

location1 = [1, 2, 3]
location2 = [4, 5, 6]
location3 = list(map(lambda coord: list(coord), zip(location1,location2)))
print(location3) 
raviraj
  • 335
  • 4
  • 19
  • yes, Case 1 was correct: `[[41.7770923949, -87.6060037796], [41.784575915, -87.6547753762], [41.7442798961, -87.5716351762]` – Gazoo Nov 19 '21 at 08:11