除了来自单元格的原始输出之外,我还想花费在单元格执行上花费的时间.
为此,我试过%%timeit -r1 -n1但它没有公开在单元格中定义的变量.
%%time 适用于仅包含1个语句的单元格.
In[1]: %%time
1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1
In[2]: %%time
# Notice there is no out result in this case.
x = 1
x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我已经在Nbextension中使用Execute Time已经有一段时间了.太棒了.
如何定义一个将带有空数组的可选数组作为默认值的函数?
public void DoSomething(int index, ushort[] array = new ushort[] {},
bool thirdParam = true)
Run Code Online (Sandbox Code Playgroud)
结果是:
'array'的默认参数值必须是编译时常量.
import pylab as plt
x = range(1, 7)
y = (220, 300, 300, 290, 320, 315)
def test(axes):
axes.bar(x,y)
axes.set_xticks(x, [i+100 for i in x])
a = plt.subplot(1,2,1)
test(a)
b = plt.subplot(1,2,2)
test(b)
Run Code Online (Sandbox Code Playgroud)

我期待xlabs为101, 102 ...
然而,如果我切换到使用plt.xticks(x, [i+100 for i in x])并明确重写函数,它的工作原理.
我试过pprint,print前者只打印Unicode版本,后者不做漂亮的打印.
from sympy import symbols, Function
import sympy.functions as sym
from sympy import init_printing
init_printing(use_latex=True)
from sympy import pprint
from sympy import Symbol
x = Symbol('x')
# If a cell contains only the following, it will render perfectly.
(pi + x)**2
# However I would like to control what to print in a function,
# so that multiple expressions can be printed from a single notebook cell.
pprint((pi + x)**2)
Run Code Online (Sandbox Code Playgroud) 我int对cython中的这么多数据类型感到有点挣扎.
np.int, np.int_, np.int_t, int
我猜int在纯python中相当于np.int_,那么它np.int来自哪里呢?我从numpy找不到文件?另外,np.int_鉴于我们已经存在,为什么存在int?
在cython中,我猜想int当它被用作cdef int或者成为C型时ndarray[int],并且当int()它被用作python施法者时它会被使用?
是np.int_相当于long用C?那cdef long是相同的cdef np.int_吗?
我应该在什么情况下使用np.int_t而不是np.int?例如cdef np.int_t,ndarray[np.int_t]......
有人可以简单解释这些类型的错误使用会如何影响已编译的cython代码的性能?
我想更改错误栏颜色.经过多次尝试后,我找到了一条路:
a = plt.gca()
b = a.bar(range(4), [2]*4, yerr=range(4))
c = a.get_children()[8]
c.set_color(['r','r','b','r'])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法?当然a.get_children()[8]不是一般的解决方案.

在单元测试中,我想将一行代码硬编码为字符串.
在C#我会这样做
var sb = new StringBuilder();
sb.AppendLine("myline1");
sb.AppendLine("myline2");
sb.AppendLine("myline3");
Run Code Online (Sandbox Code Playgroud)
自从我转换为F#后,我试图通过使用来减少.Net方法的使用bprintf,但不知何故没有bprintfn支持这对我来说很奇怪.
\r\n手动添加每行末尾很繁琐.
还是有比这更好的方法StringBuilder吗?
我检查了文件long= int64已范围超过900,000,000,000,000
这是我的代码:
int r = 99;
long test1 = r*r*r*r*r;
Run Code Online (Sandbox Code Playgroud)
在运行时它给了我919,965,907而不是正确的9,509,900,499.
另一个考验
long test2 = 99*99*99*99*99;
Run Code Online (Sandbox Code Playgroud)
它拒绝编译,说整数溢出.
但如果我这样做
long test3 = 10100200300;
Run Code Online (Sandbox Code Playgroud)
这很好用.
> fun1 <- function(x,y){x+y}
> outer(seq(1,5,length=5),seq(6,10,length=5),fun1)
[,1] [,2] [,3] [,4] [,5]
[1,] 7 8 9 10 11
[2,] 8 9 10 11 12
[3,] 9 10 11 12 13
[4,] 10 11 12 13 14
[5,] 11 12 13 14 15
> fun2 <- function(x,y){z<-c(x,y);z[1]+z[2]}
> outer(seq(1,5,length=5),seq(6,10,length=5),fun2)
Error in dim(robj) <- c(dX, dY) :
dims [product 25] do not match the length of object [1]
Run Code Online (Sandbox Code Playgroud)
为什么fun2()不起作用?不是fun2()和fun1()基本上是一回事吗?
假设我有3个char变量a,b和c.
每一个都可以'0',这是一个特例,意味着它匹配每个字符.
所以,如果是'0',我只需要检查是否b == c.
我想检查是否a == b == c,但发现C#中的实现变得混乱和冗长.
您可以提供任何创意或漂亮的解决方案吗?
更新
对于绩效驱动,采取Erik A. Brandstadmoen的方法.为简单起见,使用M4N的apprach,我也做了一些修改:!(query.Any() && query.Distinct().Skip(1).Any())