How can I remove Nan from list Python/NumPy

前端 未结 10 1913
挽巷
挽巷 2020-12-04 10:56

I have a list that countain values, one of the values I got is \'nan\'

countries= [nan, \'USA\', \'UK\', \'France\']

I tried to remove it,

10条回答
  •  渐次进展
    2020-12-04 11:24

    Using your example where...

    countries= [nan, 'USA', 'UK', 'France']

    Since nan is not equal to nan (nan != nan) and countries[0] = nan, you should observe the following:

    countries[0] == countries[0]
    False
    

    However,

    countries[1] == countries[1]
    True
    countries[2] == countries[2]
    True
    countries[3] == countries[3]
    True
    

    Therefore, the following should work:

    cleanedList = [x for x in countries if x == x]
    

提交回复
热议问题