I'm trying to separate x and y points from a doubly nested list. I'm having a hard time in separating it correctly

I'm trying to separate x and y points from a doubly nested list, The list elements are series of x and y points and I need to separate them to individual x and y list. The list is of the form [[[x1, y1, x2, y2, x3, y3.......xn, yn]]] and I need to separate it as [[[x1, x2, x3....xn]]] and [[[y1, y2, y3...yn]]]. I wrote the program below and it works fine except for one point 106.28 which should be in y list but it appears in x list and hence x list has 48 elements and y list has 46 elements. I really don't understand why both x and y list don't come as of equal length of 47 elements. I would really appreciate if someone can help me figure out why its the case.

x_y_list = [[[139.98, 69.12, 114.6, 69.12, 112.54, 70.72, 111.59, 71.61, 110.66, 72.6, 109.8, 73.65, 145.39, 73.65, 146.02, 74.35, 147.18, 75.83, 148.25, 77.38, 148.73, 78.18, 107.12, 78.19, 106.81, 78.91, 106.28, 80.41, 105.87, 81.94, 105.7, 82.72, 150.84, 82.72, 151.1, 83.45, 151.53, 84.96, 151.88, 86.48, 152.01, 87.25, 105.29, 87.25, 105.3, 88.01, 105.41, 89.53, 105.62, 91.04, 105.76, 91.78, 152.31, 91.78, 152.29, 92.55, 152.16, 94.06, 151.92, 95.57, 151.77, 96.32, 107.18, 96.32, 107.52, 97.11, 108.3, 98.65, 109.18, 100.13, 109.65, 100.85, 150.19, 100.85, 149.79, 101.66, 148.87, 103.22, 147.81, 104.69, 147.24, 105.39, 113.61, 105.38, 114.63, 106.28, 116.82, 107.91, 119.17, 109.31, 120.4, 109.92, 141.43, 109.91]]]
x_list = []
y_list = []
for i in x_y_list:
    H = []
    W = []
    for j in i:
        Q = []
        B = []
        for k in j:
            if j.index(k)%2 == 0:
                Q.append(k)
            if j.index(k)%2 == 1:
                B.append(k)
        H.append(Q)
        W.append(B)
    x_list.append(H)
    y_list.append(W)

#print(x_list)
#print(y_list)
print(len(x_list[0][0]))
print(len(y_list[0][0]))
48
46

You would be better off using a single line numpy expression for that:

import numpy as np
list_x, list_y = np.reshape(np.array(x_y_list[0][0]), (2, int(len(x_y_list[0][0])/2)))

Edit: This would replace your for k in j: loop. Your x_list[0][0] is my x_list.

Note that using numpy you could reshape your whole 3-dimensional nested list at once, no loops required.

The problem with your code is that you lookup the index using the value j.index(k). This means that as soon as you have double values in the list, you are in trouble. Therefore, you should use for ik, k in enumerate(j) instead of for k in j. This gives you directly the index ik, which you could use for if ik % 2 == 0

I (Kendrick Lamar song), I definition, the ninth letter of the English alphabet, a vowel. See more. Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family.

You can use list slicing:

lst = [[[1, 10, 2, 20, 3, 30]]]

inner = lst[0][0]
xs, ys = [[inner[::2]]], [[inner[1::2]]]

print(xs, ys)
# [[[1, 2, 3]]] [[[10, 20, 30]]]

If you have multiple sublists:

lst = [[[1, 10, 2, 20], [3, 30, 4, 40]]]

inner = lst[0]

xs = [[i[::2] for i in inner]]
ys = [[i[1::2] for i in inner]]

print(xs, ys)
# [[[1, 2], [3, 4]]] [[[10, 20], [30, 40]]]

You can also use numpy:

lst = [[[1, 10, 2, 20], [3, 30, 4, 40]]]
arr = np.array(lst)

xs = arr[:,:,::2]
ys = arr[:,:,1::2]

print(xs)
print(ys)

Output:

[[[1 2]
  [3 4]]]

[[[10 20]
  [30 40]]]

I, Windows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark of Apple Inc. In the Phoenician alphabet, the letter may have originated in a hieroglyph for an arm that represented a voiced pharyngeal fricative (/ʕ/) in Egyptian, but was reassigned to /j/ (as in English "yes") by Semites, because their word for "arm" began with that sound.

Alternative: Use appropriate ranges as indices in list comprehensions:

x_list = [x_y_list[0][0][i] for i in range(0, len(x_y_list[0][0]), 2)]
y_list = [x_y_list[0][0][i] for i in range(1, len(x_y_list[0][0]), 2)]

Edit: This would replace your for k in j: loop. Your x_list[0][0] is my x_list.

[PDF] USCIS Form I-9, Use Form I-9 to verify the identity and employment authorization of individuals hired for employment in the United States. Ï, lowercase ï, is a symbol used in various languages written with the Latin alphabet; it can be read as the letter I with diaeresis or I-umlaut.. In Afrikaans, Catalan, Dutch, French, Galician, Welsh, Southern Sami, and occasionally English, ï is used when i follows another vowel and indicates hiatus (diaeresis) in the pronunciation of such a word.

I-9, Employment Eligibility Verification, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Usage in various languages Faroese. Í is the 11th letter of the Faroese alphabet and represents /ʊi/.. Hungarian, Icelandic, Irish, Czech and Slovak. Í is the 16th letter of the Hungarian alphabet, the 12th letter of the Icelandic alphabet, the 16th letter of the Czech alphabet and the 18th letter of the Slovak alphabet.

I, Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family. The sixteenth letter of the Hungarian alphabet, called í and written in the Latin script.

Instagram, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? Bir hesap oluştur veya Instagram'a giriş yap - Fotoğraf ve videolar çekip düzenlemenin, bu fotoğrafları, videoları ve mesajları arkadaşlarınla ve akrabalarınla paylaşmanın basit, eğlenceli ve yaratıcı yolu.

Comments
  • Its a great answer! But I also would like to know the reason why my code is not producing the desired output
  • added explanation to the answer
  • Yesss, Yes.. Its the index. Thank you very much!!
  • If I have a list like [[[1,10,2,20], [6,8,4,3,5,6]]] how to use the above method?
  • @nick What is your expected result?
  • I would make a loop
  • @ Is it your expected result: xs = [1, 2, 6, 4, 5] and ys=[10, 20, 8, 3, 6]?
  • xs = [[[1,2], [6,4,5]]] and ys = [[[10, 20], [8, 3, 6]]]