删除空括号并获取值

时间:2019-07-10 02:05:10

标签: python arrays

我在下面。...我不确定这是列表列表还是列表等。

[['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 for i in (array[temp[0]).flat]

我想遍历并分别访问列表中的每个值 即

346565.09644975792617
6039234.5627173967659
102.90000149316718137

5 个答案:

答案 0 :(得分:1)

如果您的数据与显示的完全一样,则它不是可接受的数据类型。您在括号内没有用逗号分隔的字符串项目。同样,您的方括号也不用逗号分隔。

但是,如果您的数据确实有逗号但没有显示,则您有3个列表项,其中第一个和第三个列表是2维的,第2个列表是1D。如果您知道 numpy ,则可以使用output = input.reshape((row, col))。在这里,您需要使用row = 1,column =数据长度。

如果要使用列表索引,则可以使用方括号符号为每个项目建立索引。第一个数据点将是data [0,0],第二个数据点是[0,1]。空括号将使用data [1]进行索引,因为它是1D,所以只有一个索引值。 第三个列表将从data [2,0]和data [2,1]开始

答案 1 :(得分:0)

const getUserGeolocation = (): Promise<{latitude: number, longitude: number}> => new Promise(async (resolve) => {
    navigator.geolocation.getCurrentPosition(async pos => {
        resolve({latitude: pos.coords.latitude, longitude: pos.coords.longitude})
    })
})
const {latitude, longitude} = await getUserGeolocation()

然后

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

答案 2 :(得分:0)

下面的代码是我的方法。

方法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]]

方法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]

答案 3 :(得分:0)

更好:

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

除了@ninedongsu的答案,您只需执行以下操作即可:

list(filter(None, array.tolist()))

答案 4 :(得分:0)

我想出了这一点,我相信这不是最好的解决方案,但是它可以工作。 我用

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

展平然后附加到列表,以便数据看起来像这样

['346519.28163281054003','6039317.3740590326488','101.89338011475197732','346470.20544341672212','6039253.728553045541','102.94999692379150247','346515.16140150092542','6039237.1104527609423','102.81999966426547546 ','346470.20544341672212','6039253.728553045541','102.94999692379150247','346474.98413434700342','6039197.5386717105284','102.33234963360963832'],['346565.09644975792617','6039234.5627173967659','102.90000149316475' ','346515.16140150092542','6039237.1104527609423','102.81999966426546678','346565.09644975792617','6039234.5627173967659','102.90000149316718137','346515.16140150092542','6039237.1104527609423','102.81999966426546'978。 ,[],['346714.73278179299086','6039224.1555810244754','103.08000181452024435','346664.8500935 1186454','6039227.5649940613657','103.06999966149143688','346686.75602762267226','6039181.4495896659791','102.44271274879886846','346664.85009351186454','6039227.5649940613657','103.06999966149143688'984'922'346'692'3456' ,'6039268.2906331047416','102.59342433410905926']

然后我使用了解决方案

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

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