Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除空括号并获取值_Python_Arrays - Fatal编程技术网

Python 删除空括号并获取值

Python 删除空括号并获取值,python,arrays,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'
  '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]
我想逐一遍历并访问列表中的每个值 i、 e

然后


下面的代码是我的方法

方法#1

进近#2

更好的是:

print([x for x in array if x])
除了@ninedongsu的答案之外,您可以简单地执行以下操作:

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

我知道这不是最好的解决方案,但它很有效。。 我曾经

展开,然后附加到列表中,使数据如下所示

['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']

然后我使用了这个解决方案

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

from

如果数据完全如图所示,则它不是可接受的数据类型。括号内有未用逗号分隔的字符串项。此外,括号内也未用逗号分隔

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

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

嗨,Jennifer,它完全如图所示,我最终计算出来,如下所示,感谢您的时间。很好!因此您能够转换为数据点之间和括号之间带有逗号的有效列表类型。
>>> 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]]
>>> 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]
print([x for x in array if x])
list(filter(None, array.tolist()))
[i for i in (co_arr[temp]).flat]
list2 = [x for x in list1 if x != []]