Tag: python

如何正确closuresIPython Notebook?

如何正确closuresIPython Notebook? 目前,我只closures浏览器选项卡,然后在terminal中使用Ctrl+C 不幸的是, exit()或者Kill kernel upon exit勾选Kill kernel upon exit确实有帮助(他们杀死内核但不退出iPython)。

元类多inheritance不一致

为什么是这样: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyList(list, MyMixin): pass 好吧,并按预期工作: created <class '__main__.MyMixin'> created <class '__main__.MyList'> 但是这个: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyObject(object, MyMixin): pass 不好吗,这样炸了吗? created <class '__main__.MyMixin'> Traceback (most recent call […]

如何在不更改matplotlib默认值的情况下使用seaborn?

我正在尝试使用seaborn,因为它的distplot函数。 但我更喜欢默认的matplotlib设置。 当我导入海豹时,它会自动改变我的身材的外观。 我如何使用seaborn函数而不改变图的外观?

在Python中减去两次

我有两个datetime.time值, exit并enter ,我想要做的事情如: duration = exit – enter 但是,我得到这个错误: TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time 我如何正确地做到这一点? 一个可能的解决scheme是将timevariables转换为datetime timevariables,然后子结构,但我相信你们必须有一个更好,更干净的方式。

使用Python列表理解基于条件查找元素的索引

来自Matlab背景的下面的Python代码看起来很长 >>> a = [1, 2, 3, 1, 2, 3] >>> [index for index,value in enumerate(a) if value > 2] [2, 5] 在Matlab中,我可以写: >> a = [1, 2, 3, 1, 2, 3]; >> find(a>2) ans = 3 6 有没有用Python写这个简短的方法,或者我只是坚持长版本? 感谢您对Python语法基本原理的所有build议和解释。 在numpy网站上find以下内容后,我想我find了一个我喜欢的解决scheme: http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays 从该网站上的信息应用到我的问题上面,会给出以下内容: >>> from numpy import array >>> a = array([1, 2, 3, 1, […]

正确的方式来初始化一个OrderedDict使用它的构造函数,以保持初始数据的顺序?

初始化有序字典(OD)以便保留初始数据的顺序的正确方法是什么? from collections import OrderedDict # Obviously wrong because regular dict loses order d = OrderedDict({'b':2, 'a':1}) # An OD is represented by a list of tuples, so would this work? d = OrderedDict([('b',2), ('a', 1)]) # What about using a list comprehension, will 'd' preserve the order of 'l' l = ['b', 'a', 'c', […]

为什么Python对可以嵌套的静态块的数量有限制?

Python中静态嵌套块的数量限制为20个。也就是说,嵌套19 for循环将会很好(虽然过于耗时; O(n^19)是疯狂的),但是嵌套20将会失败: SyntaxError: too many statically nested blocks 有这个限制的根本原因是什么? 有没有办法增加限制?

Python SQL查询string格式

我试图find格式化SQL查询string的最佳方法。 当我debugging我的应用程序时,我想logging所有的sql查询string,重要的是string正确格式化。 选项1 def myquery(): sql = "select field1, field2, field3, field4 from table where condition1=1 and condition2=2" con = mymodule.get_connection() … 这对于打印sqlstring很有用。 如果string很长并且不符合80个字符的标准宽度,那么这不是一个好的解决scheme。 选项2 def query(): sql = """ select field1, field2, field3, field4 from table where condition1=1 and condition2=2""" con = mymodule.get_connection() … 这里的代码是清楚的,但是当你打印sql查询string,你会得到所有这些烦人的空白。 u'\ n从表中selectfield1,field2,field3,field4 \ n_ _ ___ \ n 其中condition1 […]

修改Python字典,同时迭代它

假设我们有一个Python字典d ,我们正在迭代它: for k,v in d.iteritems(): del d[f(k)] # remove some item d[g(k)] = v # add a new item ( f和g只是一些黑盒转换。) 换句话说,我们尝试添加/删除项目,而使用iteritems迭代它。 这是很好的定义? 你能提供一些参考来支持你的答案吗? (这很明显,如果它被破坏,如何解决这个问题,所以这不是我所追求的angular度。)

Imshow:范围和方面

我正在编写一个软件系统,通过3D数据集可视化切片和投影。 我正在使用matplotlib ,特别是imshow来显示我从分析代码中得到的图像缓冲区。 由于我想使用绘图轴标注图像,因此我使用imshow提供的extent关键字将图像缓冲区像素坐标映射到数据空间坐标系。 不幸的是, matplotlib不知道单位。 说(以一个人为的例子),我想绘制一个尺寸为1000 m X 1 km的图像。 在这种情况下,范围将是[0, 1000, 0, 1] 。 即使图像数组是正方形的,由于extent关键字隐含的宽高比为1000,所得的绘图轴也具有1000的宽高比。 是否可以强制的情节的纵横比,同时仍然保持自动生成的主要刻度和标签,我得到了使用关键字?