草庐IT

output_len

全部标签

python - 为什么 len() 没有为队列实现?

内置函数len()(https://docs.python.org/3/library/functions.html#len)返回“一个对象的长度(项目数)”,但这并没有实现对于queue.Queue(https://docs.python.org/3/library/queue.html)。相反,queue.Queue有一个qsize()方法,当队列明显有长度时,它返回队列的近似大小;您可以在构造函数中指定Queue的最大长度。类似的collections.deque确实适用于len。queue.Queue没有使用通用的len()的原因是什么?或者:如果将qsize改为命名为__le

python - 在 python 中,len(list) 是做什么的?

len(list)是在每次调用时计算列表的长度,还是返回内置计数器的值?我有一个上下文每次循环都需要检查列表的长度,例如:listData=[]forvalueinioread():iflen(listData)>=25:processlistdata()clearlistdata()listData.append(value)我应该在每次迭代时检查len(listData),还是应该为列表的长度设置一个计数器? 最佳答案 您可能应该知道,如果您担心此操作的性能,Python中的“列表”arereally动态数组。也就是说,它们不是

python - 为什么 len(None) 不返回 0?

None在Python中是一个对象。>>>isinstance(None,object)True因此它可以使用像__str__()这样的函数>>>str(None)'None'但为什么它不对__len__()做同样的事情呢?>>>len(None)Traceback(mostrecentcalllast):File"",line1,inlen(None)TypeError:objectoftype'NoneType'hasnolen()这似乎是Pythonic,即使变量是None而不仅仅是一个空列表,iflist也是可以接受的。是否存在使用len(None)问题更大的情况?

python - 为什么 len(<a list object>) 这么慢?

我在ipython中运行以下代码session:#Thiscallisslow,butthatisexpected.(Itloads3GBofdata.)In[3]:arc,arc_sub,upls,go=foo_mod.ready_set()#Thiscallisalsoslow,as`upls`ishuge.In[4]:upls=list(upls)#Thiscallisslowinmeatspace,but`%timeit`doesn'tnotice!In[5]:%timeit-n1-r1len(upls)1loops,bestof1:954nsperloop%timeit正直躺

python - 为什么 len 在 DataFrame 上比在底层 numpy 数组上效率高得多?

我注意到在DataFrame上使用len比在底层numpy数组上使用len快得多。我不明白为什么。通过shape访问相同的信息也没有任何帮助。当我尝试获取列数和行数时,这更相关。我一直在争论使用哪种方法。我整理了以下实验,很明显我将在数据帧上使用len。但是有人可以解释为什么吗?fromtimeitimporttimeitimportpandasaspdimportnumpyasnpns=np.power(10,np.arange(6))results=pd.DataFrame(columns=ns,index=pd.MultiIndex.from_product([['len','l

python - pytest capsys : checking output AND getting it reported?

Python3.4.1,pytest2.6.2。当测试失败时,pytest将定期报告测试打印到标准输出的内容。例如这段代码:defmethod_under_test():print("Hallo,Welt!")return41deftest_result_only():result=method_under_test()assertresult==42当作为python-mpytestmyfile.py执行时,将报告:==================================FAILURES===================================________

python - 类型错误 : 'int' object is not callable, ,, len()

我写了一个程序来玩刽子手---它还没有完成,但由于某种原因它给了我一个错误...importturtlen=Falsey=Truelist=()print("welcometothehangman!youwordis?")word=raw_input()len=len(word)forxinrange(70):printprint"_"*lenwhilen==False:whiley==True:print"insertaletter:"p=raw_input()leenghthp=len(p)ifleengthp!=1:print"youdidntgivemealetter!!!"e

python - 当 >64 位时从 __len__() 返回

在这个问题中,我处理的是IPv6网络地址空间,所以长度是2^(128-subnet)。看起来python(至少在这台机器上)最多可以处理64位有符号数作为__len__()的返回值。所以len(IP('2001::/66'))有效,但len(IP('2001::/65'))失败。fromIPyimportIPlen(IP('2001::/64'))Traceback(mostrecentcalllast):File"test.py",line2,inlen(IP('2001::/64'))OverflowError:longinttoolargetoconverttoint有问题的IP

python - subprocess.check_output() : show output on failure

此时subprocess.check_output()的输出如下所示:CalledProcessError:Command'['foo',...]'returnednon-zeroexitstatus1有没有办法获得更好的错误信息?我想查看stdout和stderr。 最佳答案 将STDERR重定向到STDOUT。示例来自口译员:>>>try:...subprocess.check_output(['ls','-j'],stderr=subprocess.STDOUT)...exceptsubprocess.CalledProces

python ,子进程: reading output from subprocess

我有以下脚本:#!/usr/bin/pythonwhileTrue:x=raw_input()printx[::-1]我从ipython调用它:In[5]:p=Popen('./script.py',stdin=PIPE)In[6]:p.stdin.write('abc\n')cba而且效果很好。但是,当我这样做时:In[7]:p=Popen('./script.py',stdin=PIPE,stdout=PIPE)In[8]:p.stdin.write('abc\n')In[9]:p.stdout.read()解释器挂起。我究竟做错了什么?我希望能够多次从另一个进程写入和读取,以将一