简体   繁体   English

从元组列表中获取带有“NaN”的元组索引

[英]Get index of tuple with “NaN” from list of tuples

I have a list of tuples with one element as NaN :我有一个元组列表,其中一个元素为NaN

l = [('a', 7.0), ('b', float('nan'))]

And I want to find the index of tuple ('b', float('nan')) in the above list.我想在上面的列表中找到元组('b', float('nan'))的索引。

l.index(('b', float('nan')) is unable to find the element in the list even though its index is 1. It is raising ValueError exception as: l.index(('b', float('nan'))无法在列表中找到该元素,即使它的索引为 1。它引发ValueError异常为:

>>> l.index(('b', float('nan'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: ('b', nan) is not in list

Most likely this is due to the fact that each float('nan') is an independent NaN object, meaning that the two tuples are different objects as well.这很可能是因为每个float('nan')都是一个独立的 NaN object,这意味着这两个元组也是不同的对象。

How do I solve this problem in general?我一般如何解决这个问题?

float('nan') == float('nan') returns False because it is designed to not to match with itself. float('nan') == float('nan')返回False ,因为它被设计为不与自身匹配。 That's why list.index() function is unable to find a match for NaN value and is raising ValueError exception.这就是为什么list.index() function 无法找到NaN值的匹配项并引发ValueError异常。

Please read Why is NaN not equal to NaN?请阅读为什么 NaN 不等于 NaN? to know more about this behaviour.了解有关此行为的更多信息。

Below is a custom function check_nan_match() to check whether passed objects are having same value or not.下面是一个自定义的 function check_nan_match()来检查传递的对象是否具有相同的值。 This function will be able to match for NaN objects too based on the above property ie NaN s return False when matched with itself .这个 function 也将能够基于上述属性匹配NaN对象,即NaN与自身匹配时返回False

# Function too check passed values are match, including `NaN`
def check_nan_match(a, b):
    return (b != b and a != a) or a == b
          # ^    ^ `NaN` property to return False when matched with itself

To get the index of tuple in the list containing NaN , here I am creating another custom function as get_nan_index .为了在包含NaNlist中获取tuple的索引,我在这里创建另一个自定义 function 作为get_nan_index This function accepts my_list and my_tuple as param, iterates over the my_list to get the index of my_tuple .这个 function 接受my_listmy_tuple作为参数,遍历my_list以获得my_tuple的索引。 To check for the equality, I am using previously created check_nan_match function which is capable to match NaN values too.为了检查相等性,我使用了之前创建check_nan_match function,它也能够匹配NaN值。

# Get index from list of tuple , when tuple is passed
def get_nan_index(my_list, my_tuple):
    for i, t in enumerate(my_list):
        if all(check_nan_match(x, y) for x, y in zip(t, my_tuple)):
            return i
    else:
        raise ValueError  # Raise `ValueError` exception in case of no match.
                          # Similar to `list.index(...)` function

Sample run:样品运行:

# check for tuple with `NaN` 
>>> get_nan_index([('a', 7.0), ('b', float('nan'))], ('b', float('nan')))
1

# check for tuple without `NaN`
>>> get_nan_index([('a', 1), ('b', 2)], ('b', 2))
1

# `ValueError` exception if no match
>>> get_nan_index([('a', 7.0), ('b', 3)], ('b', float('nan')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in get_nan_index
ValueError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM