1

I have this below....i'm not sure if it's a list of lists or just a list etc.

[['346565.09644975792617' '6039234.5627173967659' '102.90000149316718137'
  '346519.28163281054003' '6039317.3740590326488' '101.89338011475197732'
  '346515.16140150092542' '6039237.1104527609423' '102.81999966426546678']
 ['346565.09644975792617' '6039234.5627173967659' '102.90000149316718137'
  '346515.16140150092542' '6039237.1104527609423' '102.81999966426546678'
  '346537.27037519804435' '6039179.8096304181963' '102.07013642431296319']]
[]
[['346714.73278179299086' '6039224.1555810244754' '103.08000181452024435'
  '346664.85009351186454' '6039227.5649940613657' '103.06999966149143688'
  '346686.75602762267226' '6039181.4495896659791' '102.44271274879886846']
 ['346664.85009351186454' '6039227.5649940613657' '103.06999966149143688'
  '346714.73278179299086' '6039224.1555810244754' '103.08000181452024435'
  '346742.24909936846234' '6039268.2906331047416' '102.59342433410905926']]

I want to access all the values, i think i can do this if i remove the empty bracket, but i haven't been able to remove it

I've tried flattening but and a couple of other things but it comes up with index out of range when it hits the empty []

[i for i in (array[temp[0]).flat]

I want to iterate through and access each value in the list individually i.e.

346565.09644975792617
6039234.5627173967659
102.90000149316718137
sp10
  • 11
  • 1

5 Answers5

0
list2 = [x for x in list1 if x != []]

Then

for i in range(1, len(list2)):
    // do what you want with element 'i'
pedro_bb7
  • 1,601
  • 3
  • 12
  • 28
0

below code is my approach.

approach #1

>>> data = [[1,2,3], [], [4,5], [], [2,4]]
>>> filtered = list(filter(lambda x: len(x)>0, data))
>>> filtered
[[1, 2, 3], [4, 5], [2, 4]]

approach #2

>>> data = [[1,2,3], [], [4,5], [], [2,4]]
>>> res = [sum(x) for x in data if x]  # you can make the condition for list-comprehension, `sum(x)` is your logic
>>> res
[6, 9, 6]
ninedongsu
  • 23
  • 3
0

Even better:

print([x for x in array if x])

Adding to @ninedongsu's answer, you can simply do:

list(filter(None, array.tolist()))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

I figured this out I am sure it's not the best solution but it works.. i used

[i for i in (co_arr[temp]).flat]

to flatten and then appended to a list so the data looked like this

['346519.28163281054003', '6039317.3740590326488', '101.89338011475197732', '346470.20544341672212', '6039253.728553045541', '102.94999692379150247', '346515.16140150092542', '6039237.1104527609423', '102.81999966426546678', '346515.16140150092542', '6039237.1104527609423', '102.81999966426546678', '346470.20544341672212', '6039253.728553045541', '102.94999692379150247', '346474.98413434700342', '6039197.5386717105284', '102.33234963360963832'], ['346565.09644975792617', '6039234.5627173967659', '102.90000149316718137', '346519.28163281054003', '6039317.3740590326488', '101.89338011475197732', '346515.16140150092542', '6039237.1104527609423', '102.81999966426546678', '346565.09644975792617', '6039234.5627173967659', '102.90000149316718137', '346515.16140150092542', '6039237.1104527609423', '102.81999966426546678', '346537.27037519804435', '6039179.8096304181963', '102.07013642431296319'], [], ['346714.73278179299086', '6039224.1555810244754', '103.08000181452024435', '346664.85009351186454', '6039227.5649940613657', '103.06999966149143688', '346686.75602762267226', '6039181.4495896659791', '102.44271274879886846', '346664.85009351186454', '6039227.5649940613657', '103.06999966149143688', '346714.73278179299086', '6039224.1555810244754', '103.08000181452024435', '346742.24909936846234', '6039268.2906331047416', '102.59342433410905926']

then i used the solution

list2 = [x for x in list1 if x != []]

from Python: How to remove empty lists from a list?

sp10
  • 11
  • 1
0

If your data is exactly as shown, then it is not an acceptable data type. You have string items that are not separated by commas inside brackets. Also your brackets are not separated by commas.

If however your data does have commas but you did not show it, then you have 3 list items, where the first and third list is 2-dimensional, and 2nd list is 1D. If you know numpy, you can use output = input.reshape((row, col)). Here you would want to use row = 1 and column = length of your data.

If you want to use list indexing, you can index each item using bracket notation. First data point will be data[0, 0], and 2nd data[0, 1]. The empty bracket will be indexed using data[1] There is only one index value since it is 1D. The third list will start with data[2, 0] and data[2, 1]

Jennifer Yoon
  • 791
  • 5
  • 10
  • hi Jennifer, it was exactly as shown, I worked it out in the end, as below, thanks for your time. – sp10 Jul 10 '19 at 03:08
  • Good! So you were able to convert to a **valid list type** with commas between data points and between brackets. – Jennifer Yoon Jul 10 '19 at 04:03