在以下示例中
In [8]: import numpy as np
In [9]: strings = np.array(['hello ', 'world '], dtype='|S10')
In [10]: strings == 'hello'
Out[10]: array([False, False], dtype=bool)
Run Code Online (Sandbox Code Playgroud)
由于空白,比较失败.是否有一个Numpy内置函数,相当于
In [12]: np.array([x.strip()=='hello' for x in strings])
Out[12]: array([ True, False], dtype=bool)
Run Code Online (Sandbox Code Playgroud)
哪个给出了正确的结果?
我想创建一个数组dtype=np.object
,其中每个元素都是一个数字类型的数组,例如int或float.例如:
>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] = a
>>> b[1] = a
>>> b[2] = a
Run Code Online (Sandbox Code Playgroud)
创造我想要的东西:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
int64
Run Code Online (Sandbox Code Playgroud)
但我想知道是否没有办法在一行中写入第3行到第6行(特别是因为我可能想要连接100个数组).我试过了
>>> b = np.array([a,a,a],dtype=np.object)
Run Code Online (Sandbox Code Playgroud)
但这实际上将所有元素转换为np.object:
>>> print b.dtype
object
>>> print b.shape
(3,)
>>> print b[0].dtype
object
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法如何避免这种情况?
我试图确定线段(即两点之间)是否与球体相交.我对交点的位置不感兴趣,只是该段是否与球体表面相交.有没有人对这个最有效的算法是什么有任何建议?(我想知道是否有任何算法比通常的射线球交叉算法更简单,因为我对交叉位置不感兴趣)
处理文档字符串中重复内容的好方法是什么?我有许多函数采用'标准'参数,必须在docstring中解释,但是只编写一次docstring的相关部分会很好,因为这将更容易维护和更新.我天真地尝试了以下内容:
arg_a = "a: a very common argument"
def test(a):
'''
Arguments:
%s
''' % arg_a
pass
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为当我这样help(test)
做时,我看不到文档字符串.有没有办法做到这一点?
5 % 2
用Cython代码进行模运算()的最简单,最快的方法是什么?似乎使用%
会大大减慢代码的速度,那么更好的方法是这样做吗?
我需要在Python中跟踪float和int值的单位,但我不想使用像scale或其他的外部包,因为我不需要对值执行操作.相反,我想要的是能够定义具有单位属性的浮点数和整数(我不想为这么简单的东西添加新的依赖关系).我试过做:
class floatwithunit(float):
__oldinit__ = float.__init__
def __init__(self, *args, **kwargs):
if 'unit' in kwargs:
self.unit = kwargs.pop('unit')
self.__oldinit__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
但这根本不起作用:
In [37]: a = floatwithunit(1.,unit=1.)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/tom/<ipython console> in <module>()
TypeError: float() takes at most 1 argument (2 given)
Any suggestions?
Run Code Online (Sandbox Code Playgroud) 我正在使用MPI(与fortran但问题是MPI标准比任何给定语言更具体),并且特别使用缓冲的发送/接收函数isend和irecv.现在,如果我们想象以下场景:
流程0:
isend(stuff1, ...)
isend(stuff2, ...)
Run Code Online (Sandbox Code Playgroud)
过程1:
wait 10 seconds
irecv(in1, ...)
irecv(in2, ...)
Run Code Online (Sandbox Code Playgroud)
消息是按照发送顺序传递给进程1的,即如果使用的标记在所有情况下都相同,我可以确定in1 == stuff1和in2 == stuff2 吗?
我正在尝试编写一个configure.ac文件,以便生成的configure脚本搜索包含给定静态库的库目录,例如libsomething.a.我怎样才能做到这一点?目前,我只需检查一个位置:
AC_CHECK_FILE([/usr/local/lib/libsomething.a],[AC_SUBST(libsomething,"-L/usr/local/lib -lsomething")],[AC_SUBST(libcfitsio,'')])
Run Code Online (Sandbox Code Playgroud)
但是我想让它尝试自动找到它.如果库不在默认位置之一,我想配置为找不到库,并且可以通常使用--use-something = path指定自定义位置.所以我还需要检查是否提供了--use-something = path.我是创建配置文件的新手,并且M4文档不是很容易理解,所以非常感谢任何帮助.
谢谢!
假设我使用jQuery将新内容加载到特定的DIV元素中.如果我现在想从DIV元素中捕获事件,我认为DOM必须以某种方式更新?处理这个问题的最佳方法是什么?
编辑:这是我的意思的一个例子:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
$(document).ready(function(){
$(".edit").click(function(){
$(this).parent().load("edit");
});
$(".view").click(function(){
$(this).parent().load("view");
});
});
</script>
</head>
<body>
<div class='someid'>
<input type='submit' class='edit' value='Edit'>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
edit
同一级别的文件包含的位置
<input type='submit' class='view' value='View'>
Run Code Online (Sandbox Code Playgroud)
和一个文件'view'包含
<input type='submit' class='edit' value='Edit'>
Run Code Online (Sandbox Code Playgroud)
如果你试试这个,你会看到当你第一次按下编辑时,按钮变为视图,但它不会再改变.为什么是这样?
我有两个Numpy记录数组具有完全相同的字段.将它们组合成一个(即将一个表附加到另一个表)的最简单方法是什么?
我正在尝试编写一个在被调用后"刷新"的装饰器,但是在最后一个函数退出后只刷新一次.这是一个例子:
@auto_refresh
def a():
print "In a"
@auto_refresh
def b():
print "In b"
a()
Run Code Online (Sandbox Code Playgroud)
如果a()
被调用,我希望退出后运行刷新功能a()
.如果b()
被调用,我希望刷新函数在退出b()
后运行,而不是在a()
调用之后运行b()
.以下是执行此操作的类的示例:
class auto_refresh(object):
def __init__(self, f):
print "Initializing decorator"
self.f = f
def __call__(self, *args, **kwargs):
print "Before function"
if 'refresh' in kwargs:
refresh = kwargs.pop('refresh')
else:
refresh = False
self.f(*args, **kwargs)
print "After function"
if refresh:
print "Refreshing"
Run Code Online (Sandbox Code Playgroud)
有了这个装饰,如果我跑
b()
print '---'
b(refresh=True)
print '---'
b(refresh=False)
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Initializing decorator
Initializing decorator
Before function …
Run Code Online (Sandbox Code Playgroud)