ImmortalxR
ImmortalxR

Reputation: 329

Removing Indices from a list in Python

I am trying to get an output such as this:

169.764569892,  572870.0,  19.6976

However I have a problem because the files that I am inputing have a format similar to the output I just showed, but some line in the data have 'nan' as a variable which I need to remove. I am trying to use this to do so:

TData_Pre_Out = map(itemgetter(0, 7, 8), HDU_DATA) 
TData_Pre_Filter = [Data for Data in TData_Pre_Out if Data != 'nan']

Here I am trying to use list comprehension to get the 'nan' to go away, but the output still displays it, any help on properly filtering this would be much appreciated.

EDIT: The improper output looks like this:

169.519361471,  nan,  nan

instead of what I showed above. Also, some more info:1) This is coming from a special data file, not a text file, so splitting lines wont work. 2) The input is exactly the same as the output, just mapped using the map() line that I show above and split into the indices I actually need (i.e. instead of using all of a data list like L = [(1,2,3),(3,4,5)] I only pull 1 and 3 from that list, to give you the gist of the data structure) The Data is read in as so:

with pyfits.open(allfiles) as HDU:
HDU_DATA = HDU[1].data

The syntax is from a specialized program but you get the idea

Upvotes: 0

Views: 108

Answers (3)

zhangyangyu
zhangyangyu

Reputation: 8610

TData_Pre_Out = map(itemgetter(0, 7, 8), HDU_DATA) 

This statement gives you a list of tuples. And then you compare the tuple with a string. All the != comparisions success.

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 113975

Based on my understanding of your description, this could work

with open('path/to/file') as infile:
    for line in infile:
        vals = line.strip().split(',')
        print[v for v in vals if v!='nan']

Upvotes: 0

tamasgal
tamasgal

Reputation: 26269

Without showing how you read in your data, the solution can only be guessed.

However, if HDU_DATA stores real NaN values, try following:

Comparing variable to NaNs does not work with the equality operator ==:

foo == nan

where nan and foo are both NaNs gives always false.

Use math.isnan() instead:

import math
...if math.isnan(Data)…

Upvotes: 1

Related Questions