mercredi 19 juillet 2017

How can I add only non zero content from text file?

I have the following type of data in a text file.

15  1  
23  0  
39 -1  
71 -1  
79  1  
95  1  
127 -2  
151  2  
183  1  
191 -1  
239  0  
247  3  

I want to create a 2d list from the text file as follows. I am able to do that with the code given below with the following result

[[15, 1.36896146582243],  
[23, 0.000000000000000],  
[39, 0.848993860380692],  
[71, 0.629227476540724],  
[79, 0.596517662620081],  
[95, 0.543970127117099],  
[127, 1.88189324753006],  
[151, 1.72587115688942],  
[183, 0.391932527534896],  
[191, 0.383636720228727]]  

However I do not want all the entries, I want only those with non zero entries in 2nd column in my source text file. For example I do not want the entries

23  0
239 0

How can I add the conditional statement into my code.

with open("path.text") as file:
    R = [[int(x) for x in line.split()] for line in file]

Aucun commentaire:

Enregistrer un commentaire