lundi 26 décembre 2016

for loop in Python taking too long for giving output

I have three excel files, Book1, Book2, Book3, with me. Each one of them consists of 11447 rows and 10335 columns. And each cell contains a numeric value of an observation. Now I have a 3 tuple, (100, 150, 150) and I want to compare the numeric values of each cell of Book1 with 1st tuple (100) and of Book2 with 2nd tuple (150) and similarly Book3 with 3rd tuple (150). Now whenever the corresponding cells of these excel files match with this tuple, I want to print 1 otherwise 0. That is, say my (10,200) cell in Book1 contains 100, in Book2 the cell (10,200) contains 150 and in (10,200) cell of Book3 we have 150, then I want to print 1 else 0.

So this is the program I wrote for this.

 import xlrd

 file_loc1 = "D:\Python\Book1.xlsx"
 file_loc2 = "D:\Python\Book2.xlsx"
 file_loc3 = "D:\Python\Book3.xlsx"

 workbook1 = xlrd.open_workbook(file_loc1)
 workbook2 = xlrd.open_workbook(file_loc2)
 workbook3 = xlrd.open_workbook(file_loc3)

sheet1 = workbook1.sheet_by_index(0)
sheet2 = workbook2.sheet_by_index(0)
sheet3 = workbook3.sheet_by_index(0)

for i in range(1,11447):
   for j in range(0,10334): 
      if sheet1.cell_value(i,j) == 100 and sheet2.cell_value(i,j) == 150 and sheet3.cell_value(i,j) == 150:
        print 1
     else:
        print 0

Firstly, I want to make sure if this program is correct or there is some issue with this? The range of loop is the one I required.

Secondly, I ran this program on my system and it has been around 10 hours and the program is still running. I am using 64-bit Python 2.7.13 on my 64-bit Windows 8.1 system. For executing, I am using Windows Powershell. I gave the following command for execution python script1.py > output1.txt as I also want an output in text. I got a text file generated in my Python directory named output1 but its size has been 0 bytes since the beginning of program. So, I am not even sure if I am getting any proper file or not. What should I do here? Is there any more efficient way to get such an output? Also, how long am I suppose to wait for this program/loop to finish up?

Aucun commentaire:

Enregistrer un commentaire