Tag: python

NumPy中的加权标准差?

numpy.average()有一个权重选项,但是numpy.std()不是。 有没有人有解决方法的build议?

获取一个类的属性

我想获得一个类的属性,说: class MyClass(): a = "12" b = "34" def myfunc(self): return self.a 使用MyClass.__dict__给了我一个属性和函数的列表,甚至像__module__和__doc__这样的函数。 虽然MyClass().__dict__给我一个空的字典,除非我明确设置该实例的属性值。 我只是想要的属性,在上面的例子是: a和b

用于检查Python属性的get()方法

如果我有字典dict并且想检查dict['key']我可以在try块(bleh!)中使用,或者使用get()方法,使用False作为默认值。 我想为object.attribute做同样的事情。 也就是说,如果没有设置,我已经有了返回False对象,但是那样会给我带来错误 AttributeError:'bool'对象没有属性'attribute'

如何在使用cx_freeze时捆绑其他文件?

我在Windows系统上使用Python 2.6和cx_Freeze 4.1.2。 我创build了setup.py来构build我的可执行文件,一切正常。 当cx_Freeze运行时,它将所有内容移动到构build目录。 我有一些其他的文件,我想包括在我的build立目录。 我怎样才能做到这一点? 这是我的结构。 src\ setup.py janitor.py README.txt CHNAGELOG.txt helpers\ uncompress\ unRAR.exe unzip.exe 这是我的片段: build立 ( name='Janitor', version='1.0', description='Janitor', author='John Doe', author_email='john.doe@gmail.com', url='http://www.this-page-intentionally-left-blank.org/', data_files = [ ('helpers\uncompress', ['helpers\uncompress\unzip.exe']), ('helpers\uncompress', ['helpers\uncompress\unRAR.exe']), ('', ['README.txt']) ], executables = [ Executable\ ( 'janitor.py', #initScript ) ] ) 我似乎无法得到这个工作。 我需要一个MANIFEST.in文件吗?

Python设置Union和Set Intersection的操作有所不同吗?

我正在做一些Python操作,我注意到一些奇怪的事情。 >> set([1,2,3]) | set([2,3,4]) set([1, 2, 3, 4]) >> set().union(*[[1,2,3], [2,3,4]]) set([1, 2, 3, 4]) 这是好的,预期的行为 – 但交叉口: >> set([1,2,3]) & set([2,3,4]) set([2, 3]) >> set().intersection(*[[1,2,3], [2,3,4]]) set([]) 我在这里迷失了方向吗? 为什么set.intersection()没有像我期望的那样运行? 我如何做联合的许多集合(假设[[1,2,3], [2,3,4]]有更多的列表)? “pythonic”的方式是什么?

很快得到文件夹的总大小

我想快速find使用Python的任何文件夹的总大小。 import os from os.path import join, getsize, isfile, isdir, splitext def GetFolderSize(path): TotalSize = 0 for item in os.walk(path): for file in item[2]: try: TotalSize = TotalSize + getsize(join(item[0], file)) except: print("error with file: " + join(item[0], file)) return TotalSize print(float(GetFolderSize("C:\\")) /1024 /1024 /1024) 这是我写的简单的脚本来获取文件夹的总大小,大约需要60秒(+ -5秒)。 通过使用多处理,我把它在四核机器上减less到23秒。 使用Windows文件浏览器只需要约3秒钟(右键单击属性以查看自己)。 那么有没有更快的方式来find一个文件夹的总大小接近的速度,Windows可以做到这一点? Windows 7中,python2.6(search,但大多数时候人们使用了一个非常类似的方法我自己)先谢谢了。

如何使用Python ftplib通过FTP下载文件

我有以下代码可以轻松连接到FTP服务器并打开一个zip文件。 我想将该文件下载到本地系统中。 怎么做? # Open the file for writing in binary mode print 'Opening local file ' + filename file = open(filename, 'wb') # Download the file a chunk at a time # Each chunk is sent to handleDownload # We append the chunk to the file and then print a '.' for progress # […]

更改matplotlib中的x轴比例

我使用Matlab创build了这个图 使用matplotlib,x-axies会绘制大的数字,例如100000,200000,300000.我想要有一些像1,2,3和10 ^ 5来表明它实际上是100000,200000,300000。 有一个简单的方法来创build这样的规模在matplotlib?

自定义Python列表sorting

我正在重构我的一些旧的代码,碰到这个: alist.sort(cmp_items) def cmp_items(a, b): if a.foo > b.foo: return 1 elif a.foo == b.foo: return 0 else: return -1 代码的作品(我写了3年前),但我无法find这个东西logging在Python文档中的任何地方,每个人都使用sorted()来实现自定义sorting。 有人可以解释为什么这个工程?

在Python中打印数字的表示forms

我想打印数字的位表示forms到控制台上,以便我可以看到所有正在完成的操作。 我怎么可能在Python中做到这一点?