for循环花费太长的时间来产生/输出Python的输出 [英] for loop taking too long to produce/export output in Python

查看:410
本文介绍了for循环花费太长的时间来产生/输出Python的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是前一个问题的延续 for循环花了很长时间来产生输出,我今天早些时候问。正如我在一个评论中告诉我的,我用 pandas 来读取excel文件来代替 xlrd 。这里是我写的程序 -

 导入pandas作为pd 
导入numpy作为np

no_of_columns = 10000

Book1 = pd.read_excel(D:\Python\Book1.xlsx,header = None,name = range(no_of_columns))
Book2 = pd .read_excel(D:\ Python\Book2.xlsx,header = None,name = range(no_of_columns))
Book3 = pd.read_excel(D:\ Python\Book3.xlsx, header = None,name = range(no_of_columns))


为范围内(1,11001):
为范围内的j(0,10000):$ b $如果Book1.iloc [i,j] == 100和Book2.iloc [i,j] == 150和Book3.iloc [i,j] == 150:
print 1
else:
print 0

但是这也没有解决我所遇到的问题。该程序仍在运行(已经5个小时),我输出在我的目录中的文本输出仍然是大小0字节。再一次,这个程序有什么问题吗?为什么从执行开始就得到一个大小相同的文件?我在 R 上运行了这样的大循环,但是每当我开始以文本或excel格式导出输出时,我的目录中会有一个文件,随着循环的进行,文件的大小会不断增加。那么为什么这里没有发生?我应该在这里做什么?

解决方案

IIUC:

(Book1.eq(100)& Book2.eq(150)& Book3.eq(150) ))* 1






p>

我也是为了美观而写的。为了复制你所做的,并得到一个CSV的结果,我会这样做:

 (Book1.eq(100 )& Book2.eq(150)&Book3.eq(150))。stack().mul(1)\ 
.to_csv('filename.csv',header = False,index = False)


This question is a continuation of a previous question for loop taking too long to produce output that I asked earlier today. As advised to me in one comment, I used pandas for reading excel files in place of xlrd. Here is the program that I wrote -

   import pandas as pd
   import numpy as np

   no_of_columns = 10000

   Book1 = pd.read_excel("D:\Python\Book1.xlsx",header=None,name=range(no_of_columns))
   Book2 = pd.read_excel("D:\Python\Book2.xlsx",header=None,name=range(no_of_columns))
   Book3 = pd.read_excel("D:\Python\Book3.xlsx",header=None,name=range(no_of_columns))


   for i in range(1,11001):
      for j in range(0,10000):
         if Book1.iloc[i,j] == 100 and Book2.iloc[i,j] == 150 and Book3.iloc[i,j] == 150:
            print 1
         else:
            print 0

But this also didn't solved the problems that I am having. The program is still running (it has been 5 hours) and the text output that I am exporting in my directory is still of size 0 bytes. Again, is there anything wrong with the program? Why am I getting a file whose size has been the same since the beginning of the execution? I have ran such kind of large loops on R but each time I started to export my output in text or in excel format, I get a file in my directory whose size continues to increase as the loop progresses. So why this isn't happening here? What should I do here?

解决方案

IIUC:

Assuming all books are same size

(Book1.eq(100) & Book2.eq(150) & Book3.eq(150)) * 1


Response to comment:

I wrote this for aesthetics as well. To replicate what you've done and get results to a csv, I'd do this:

(Book1.eq(100) & Book2.eq(150) & Book3.eq(150)).stack().mul(1) \
    .to_csv('filename.csv', header=False, index=False)

这篇关于for循环花费太长的时间来产生/输出Python的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆