我有一个python字典对象,包含每个键的布尔值,例如:
d = {'client1': True, 'client2': False}
Run Code Online (Sandbox Code Playgroud)
计算字典中True值数量的最简单,最简洁的方法是什么?
假设我有一个2D Numpy数组:
>>> a = np.random.random((4,6))
Run Code Online (Sandbox Code Playgroud)
我想为每一行添加一维数组:
>>> c = np.random.random((6,))
>>> a + c
Run Code Online (Sandbox Code Playgroud)
这有效.现在,如果我尝试向每列添加一维数组,我会收到一个错误:
>>> b = np.random.random((4,))
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)
我可以通过使用np.newaxis:
>>> a + b[:,np.newaxis]
Run Code Online (Sandbox Code Playgroud)
哪个确实有效.
什么是形状匹配规则,以避免必须使用np.newaxis?这是numpy形状元组的最后一个元素必须匹配吗?此规则是否也适用于更高维度?例如,以下工作:
>>> a = np.random.random((2,3,4,5))
>>> b = np.random.random((4,5))
>>> a + b
Run Code Online (Sandbox Code Playgroud)
所以我的问题是这是否记录在任何地方,如果它是一个可以依赖的行为,或者是否最好总是使用np.newaxis?
说我有两个Python花车a和b,有一种简单的方法来找出多少表现的实数是IEEE-754表示两个(或任何代表使用的机器使用)之间?
我正在尝试使用Numpy为整数和浮点数生成随机的64位整数值,在该类型的整个有效值范围内.要生成随机的32位浮点数,我可以使用:
In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10)
Out[2]:
array([ 1.47351436e+37, 9.93620693e+37, 2.22893053e+38,
-3.33828977e+38, 1.08247781e+37, -8.37481260e+37,
2.64176554e+38, -2.72207226e+37, 2.54790459e+38,
-2.47883866e+38])
Run Code Online (Sandbox Code Playgroud)
但如果我尝试将其用于64位数字,我会得到
In [3]: np.random.uniform(low=np.finfo(np.float64).min,high=np.finfo(np.float64).max,size=10)
Out[3]: array([ Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf])
Run Code Online (Sandbox Code Playgroud)
类似地,对于整数,我可以成功生成随机32位整数:
In [4]: np.random.random_integers(np.iinfo(np.int32).min,high=np.iinfo(np.int32).max,size=10)
Out[4]:
array([-1506183689, 662982379, -1616890435, -1519456789, 1489753527,
-604311122, 2034533014, 449680073, -444302414, -1924170329])
Run Code Online (Sandbox Code Playgroud)
但是64位整数不成功:
In [5]: np.random.random_integers(np.iinfo(np.int64).min,high=np.iinfo(np.int64).max,size=10)
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
/Users/tom/tmp/<ipython console> in <module>()
/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.random_integers (numpy/random/mtrand/mtrand.c:6640)()
/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:5813)()
OverflowError: long int too large to convert to …Run Code Online (Sandbox Code Playgroud) 作为我正在编写的程序的一部分,我需要准确地求解一个三次方程式(而不是使用数字根查找器):
a*x**3 + b*x**2 + c*x + d = 0.
Run Code Online (Sandbox Code Playgroud)
我正试图从这里使用方程式.但是,请考虑以下代码(这是Python,但它是非常通用的代码):
a = 1.0
b = 0.0
c = 0.2 - 1.0
d = -0.7 * 0.2
q = (3*a*c - b**2) / (9 * a**2)
r = (9*a*b*c - 27*a**2*d - 2*b**3) / (54*a**3)
print "q = ",q
print "r = ",r
delta = q**3 + r**2
print "delta = ",delta
# here delta is less than zero so we use the second set of equations from …Run Code Online (Sandbox Code Playgroud) 在Fortran中将变量设置为+ Infinity最安全的方法是什么?目前我正在使用:
program test
implicit none
print *,infinity()
contains
real function infinity()
implicit none
real :: x
x = huge(1.)
infinity = x + x
end function infinity
end program test
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法?
我遇到了两个必须使用相同模块名称导入的Python模块,例如
import foo
Run Code Online (Sandbox Code Playgroud)
我知道我想要的那个提供某些功能(例如foo.bar()),那么有没有办法循环使用相同名称的模块,直到找到提供这些功能的模块为止?或者除了在安装之前重命名模块之外没有办法吗?
编辑:只是为了澄清我的意思,两个模块都在网站包内:
site-packages$ ls python_montage-0.9.3-py2.6.egg
EGG-INFO montage
site-packages$ ls montage-0.3.2-py2.6.egg/
EGG-INFO montage
Run Code Online (Sandbox Code Playgroud) 我最近在使用eg创建Numpy对象数组时遇到了问题
a = np.array([c], dtype=np.object)
Run Code Online (Sandbox Code Playgroud)
其中c是某个复杂类的实例,在某些情况下,Numpy尝试访问该类的某些方法.但是,做:
a = np.empty((1,), dtype=np.object)
a[0] = c
Run Code Online (Sandbox Code Playgroud)
解决了这个问题.我很好奇内部这两者之间的区别.为什么在第一种情况下Numpy会尝试访问某些属性或方法c?
编辑:为了记录,这里是演示该问题的示例代码:
import numpy as np
class Thing(object):
def __getitem__(self, item):
print "in getitem"
def __len__(self):
return 1
a = np.array([Thing()], dtype='object')
Run Code Online (Sandbox Code Playgroud)
这打印出getitem两次.基本上如果__len__在类中存在,那么这就是人们可能遇到意外行为的时候.
Python中是否有任何包允许对球体表面的经度/纬度进行类似kdtree的操作?(这需要适当考虑球面距离,以及经度环绕).
有没有人知道任何可以自动将sourceforge错误/功能请求跟踪器中的票证迁移到GitHub问题的脚本/包?可以将sourceforge票据导出为XML,因此我可以想象应该可以编写一个脚本来自动在GitHub中创建票证,所以只是想检查这样的事情是否已经存在?