列表理解与逻辑索引

2024-09-30 16:21:08 发布

您现在位置:Python中文网/ 问答频道 /正文

从Matlab慢慢过渡到Python。。。

我有这张单子

list1 = [[1, 2, nan], [3, 7, 8], [1, 1, 1], [10, -1, nan]] 

以及另一个项目数相同的列表

list2 = [1, 2, 3, 4]

我试图提取list1中不包含任何nan值的元素,list2中的相应元素,即结果应该是:

list1_clean = [[3, 7, 8], [1, 1, 1]]
list2_clean = [2, 3]

在Matlab中,这很容易通过逻辑索引完成。

在这里,我有一种感觉,对某种形式的列表理解可以做到这一点,但我坚持:

list1_clean = [x for x in list1 if not any(isnan(x))]

这显然对list2没有用处。

或者,以下逻辑索引尝试不起作用(“索引必须是整数,而不是列表”)

idx = [any(isnan(x)) for x in list1]
list1_clean = list1[idx]
list2_clean = list2[idx]

我确定这是痛苦的琐碎,但我想不出来,帮助感激!


Tags: inclean元素列表forany逻辑nan
3条回答

您可以使用zip

zip从传递给它的iterables返回同一索引上的项。

>>> from math import isnan
>>> list1 = [[1, 2, 'nan'], [3, 7, 8], [1, 1, 1], [10, -1,'nan']]
>>> list2 = [1, 2, 3, 4]
>>> out = [(x,y)  for x,y in zip(list1,list2) 
                                         if not any(isnan(float(z)) for z in x)]

>>> out
[([3, 7, 8], 2), ([1, 1, 1], 3)]

现在解压缩out以获得所需的输出:

>>> list1_clean, list2_clean = map(list, zip(*out))
>>> list1_clean
[[3, 7, 8], [1, 1, 1]]
>>> list2_clean
[2, 3]

有关zip的帮助:

>>> print zip.__doc__
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences.  The returned list is truncated
in length to the length of the shortest argument sequence.

如果需要一个内存高效的解决方案,可以使用itertools.izip,因为它返回一个迭代器。

您只需执行以下操作:

ans = [(x,y) for x,y in zip(list1,list2) if all(~isnan(x))]

#[(array([ 3.,  7.,  8.]), 2), (array([ 1.,  1.,  1.]), 3)]

从中可以提取每个值:

l1, l2 = zip(*ans) 

#l1 = (array([ 3.,  7.,  8.]), array([ 1.,  1.,  1.]))
#l2 = (2,3)

建议使用itertools模块中的izip,它使用迭代器,可以根据您的问题节省大量内存。

代替~,您可以使用numpy.logical_not(),这可能更具可读性。

欢迎来到Python!

这应该管用。我们检查一个数字是NaN还是不使用^{}

如果原始列表中的元素都不是NaN,我们将元素插入list1_cleanlist2_clean。为了检查这一点,我们使用^{}函数,如果iterable的任何元素是True,则返回True

>>> list1 = [[1, 2, float('NaN')], [3, 7, 8], [1, 1, 1], [10, -1, float('NaN')]]
>>> list2 = [1, 2, 3, 4]
>>> from math import isnan
>>> list1_clean = [elem for elem in list1 if not any([isnan(element) for element in elem])]
>>> list1_clean
[[3, 7, 8], [1, 1, 1]]
>>> list2_clean = [list2[index] for index, elem in enumerate(list1) if not any([isnan(element) for element in elem])]
>>> list2_clean
[2, 3]

为了缩小它并避免使用zip您可以这样做

>>> cleanList = [(elem, list2[index]) for index, elem in enumerate(list1) if not any([isnan(element) for element in elem])]
>>> cleanList
[([3, 7, 8], 2), ([1, 1, 1], 3)]
>>> list1_clean = [elem[0] for elem in cleanList]
>>> list2_clean = [elem[1] for elem in cleanList]

any函数->

any(...)
    any(iterable) -> bool

    Return True if bool(x) is True for any x in the iterable.

isnan函数->

isnan(...)
    isnan(x) -> bool

    Check if float x is not a number (NaN).

相关问题 更多 >