0

I had array from processed data that i get in Internet which i copied and the result is data=[[655.0, 48.77, 6.0, '1'], 0.0, [655.0, 48.77, 5.0, '1'], 1.0, [657.0, 49.25, 5.0, '4'], 2.2870067774276484, [657.0, 49.25, 1.0, '1'], 5.40651458890106, [657.0, 49.25, 1.0, '5'], 5.40651458890106]

I just confuse how I read that array. I want to copy an array of data to become a new one like

new = [['1',0.0],['1',1.0],['4',2.287006],['1',5.4065145],['5',5.4065145]]

Best regard.

3
  • so you want a final string from each nested array, and the element (not inside the nested array) right after..? Commented Jun 28, 2020 at 5:08
  • Start by explaining in words which parts of data you want to extract and put in new. For your own purposes, feel free to use whatever spoken or written language you know best. Commented Jun 28, 2020 at 5:08
  • I want to extract as i said , there a number what i called it label which include (1,4,5) and i wanna mix that label with 0.0 , 1.0, 2.28700, 5.406 and 5.406 which i called it distance into new array. Thanks sorry for bad english Commented Jun 28, 2020 at 5:47

1 Answer 1

2

Lets assume that you deal with a constant order of the data structure like the list you provided in your example.

If i understand well :

You got an input list of nested lists and some floats.

[ [ A, B, C, D ], somefloat, [A, B, C, D ] somefloat ]

And you try to get a list of nested lists which take the last element of the first nested list and combine it with the next float into a new nested list like so :

[ [D, somefloat], [D, somefloat] ]

One solution could be to use list comprehension, like so :

data=[[655.0, 48.77, 6.0, '1'], 0.0, [655.0, 48.77, 5.0, '1'], 1.0, [657.0, 49.25, 5.0, '4'], 2.2870067774276484, [657.0, 49.25, 1.0, '1'], 5.40651458890106, [657.0, 49.25, 1.0, '5'], 5.40651458890106]

newdata = [[data[i][-1], data[i +1]] for i in range(0, len(data), 2)]
print(newdata)

Output

[['1', 0.0], ['1', 1.0], ['4', 2.2870067774276484], ['1', 5.40651458890106], ['5', 5.40651458890106]]

Let's break it down :

for i in range(0, len(data), 2) 
# will iterate through the first list, the range method here (starts from 0 to the length of the input list) will return the current index as "i" with step of two, for each iteration.

[data[i][-1]
#will take the current item (which is a nested list) and extract from it the last element, feel free to change the -1 by any index of the element you desire (if you decide to finally go with the third element, so change the -1 -which mean the last element- by 2 -which is the third element


data[i +1]
# will take the next element (which is the float number)

[data[i][-1], data[i +1]
# will put them in a nested list (feel free to use tuple or dict if you want to output another kind of nested data structure)
Sign up to request clarification or add additional context in comments.

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.