小编Eri*_*ric的帖子

为什么getdate(0)作为小时返回1?

我正在使用getdate函数,我打算用它来解析一个时间间隔,以秒为单位,分为几天,几分钟,几小时等.但是,我很惊讶这getdate(0)['hours']是1.这里发生了什么?

FWIW,我得到的完整输出是:

Array
(
    [seconds] => 0
    [minutes] => 0
    [hours] => 1
    [mday] => 1
    [wday] => 4
    [mon] => 1
    [year] => 1970
    [yday] => 0
    [weekday] => Thursday
    [month] => January
    [0] => 0
)
Run Code Online (Sandbox Code Playgroud)

php datetime

2
推荐指数
1
解决办法
171
查看次数

Python - 在__hash__方法定义中使用默认的__hash__方法

我有一个类,我希望__hash__()为这个类编写一个方法.我想写的方法将在某些情况下返回对象的默认哈希值,并在其他一些情况下返回其属性之一的哈希值.所以,作为一个简单的测试案例:

class Foo:
    def __init__(self, bar):
        self.bar = bar
    def __hash__(self):
        if self.bar < 10:
            return hash(self) # <- this line doesn't work
        else:
            return hash(self.bar)
Run Code Online (Sandbox Code Playgroud)

这个问题就是hash(self)简单地调用self.__hash__(),导致无限递归.

我收集到一个对象的哈希是基于该对象的哈希id(),所以我可以重写return hash(self)return id(self),或者return id(self) / 16,但是在我自己的代码中重新创建默认实现似乎是不好的形式.

我也想到我可以把它重写为return object.__hash__(self).这可行,但似乎更糟糕,因为不打算直接调用特殊方法.

所以,我要问的是; 有没有办法使用对象的默认哈希而不隐式调用该__hash__()对象是该实例的类的方法?

python methods hash magic-methods

2
推荐指数
1
解决办法
1388
查看次数

如何将方法添加到标准java类

有没有办法将方法添加到标准Java类,例如Object

我知道你不能将类String作为final的子类,但Object不是.我知道我可以只是继承Object并在子类中定义一个方法,并使我的所有类都成为其子类,但我宁愿不必这样做.

我怀疑这是不可能的,或者我忽略了一些东西.提前感谢您的任何答案.

java methods monkeypatching class

2
推荐指数
1
解决办法
1135
查看次数

如何重新加载此模块?

from mypackage.pkg import mymodule
...
reload(mypackage.pkg.mymodule)
Run Code Online (Sandbox Code Playgroud)

结果是 NameError: global name 'mypackage' is not defined.

如何重新加载mymodule?

python module reload

2
推荐指数
1
解决办法
55
查看次数

蟒蛇.使用随机命令生成随机+或 - 符号

什么是产生最简单的方法随机+,-,*,或/登录使用导入随机函数,而这个分配的一封信.

例如

g = answeryougive('+','-')
Run Code Online (Sandbox Code Playgroud)

提前致谢 :)

python python-2.7

2
推荐指数
1
解决办法
5561
查看次数

FOR LOOP多个发生器

理解下面的代码如何工作的问题.我是Scala的新手,现在已经学习了两周的语言.

for (i <- 1 to 3; j <- 1 to 3) print((10 * i + j) + " ")
Run Code Online (Sandbox Code Playgroud)

它打印11 12 13 21 22 23 31 32 33.它是否依次分配每个值

i = 1

j = 0
Run Code Online (Sandbox Code Playgroud)

或者每次循环时都会分配值,即

i = 1

j = 1
Run Code Online (Sandbox Code Playgroud)

谢谢

scala

2
推荐指数
1
解决办法
2001
查看次数

为什么重载赋值运算符不会被继承?

为什么这段代码:

class X {
public:
    X& operator=(int p) {
        return *this;
    }
    X& operator+(int p) {
        return *this;
    }
};

class Y : public X { };

int main() {
    X x;
    Y y;
    x + 2;
    y + 3;
    x = 2;
    y = 3;
}
Run Code Online (Sandbox Code Playgroud)

给出错误:

prog.cpp: In function ‘int main()’:
prog.cpp:14:9: error: no match for ‘operator=’ in ‘y = 3’
prog.cpp:14:9: note: candidates are:
prog.cpp:8:7: note: Y& Y::operator=(const Y&)
prog.cpp:8:7: note:   no known conversion for argument 1 …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance operator-overloading

2
推荐指数
1
解决办法
4174
查看次数

为什么没有赋值的三元运算符会返回预期的输出?

我想知道为什么与三元运算符的赋值反应奇怪:

a = "foo"­
=> "foo"
a = nil ? nil : a
=> "foo"
a
=> "foo"
Run Code Online (Sandbox Code Playgroud)

但:

a = nil ? nil : a
=> "foo"
a = "bar"­ ? "bar"­ : a
=> "bar"
a
=> "bar"
Run Code Online (Sandbox Code Playgroud)

和:

if a = nil
  puts "should be nil"
end
=> nil
Run Code Online (Sandbox Code Playgroud)

不会puts是字符串,因为虽然作业成功,但a = nil会返回nil错误.

这一切都表现得像预期的那样吗?

ruby if-statement variable-assignment conditional-operator

2
推荐指数
2
解决办法
6054
查看次数

Python中的特殊张量收缩

我需要执行一种特殊类型的张量收缩.我想要这样的东西:

A_ {bg} = Sum_ {a,a',a''}(B_ {a} C_ {a'b} D_ {a''g})

其中所有指数可以有超过一个,一个值0,1和总和 '和 '' 进行对所有情况,其中A + A '+ A''= 1或A + A '+ A'" = 2.所以它就像爱因斯坦求和公约的反面:我只想在三个指数中的一个与其他指数不同时求和.

此外,我要一些挠性与不被求和索引的数目:在本例中得到的张量具有2个索引,并且总和是在3张量,一个带一个索引元件的产品的另外两个与两个索引.这些索引的数量会有所不同,所以一般来说我希望能写出这样的东西:

A _ {...} = Sum_ {a,a',a''}(B_ {a ...} C_ {a ...} D_ {a''...}})

我想指出索引的数量不是固定的,但是它是受控制的:我可以知道并指定每个张量中每个张量有多少个索引.

我试过np.einsum(),但显然我被迫在标准的爱因斯坦惯例中总结重复的指数,我不知道如何实现我在这里暴露的条件.

而且我不能用各种各样的东西写出来,因为正如我所说,所涉及的张量的索引数量并不固定.

有人有想法吗?


来自评论:

我会像这样编写我在这里编写的语言:

tensa = np.zeros((2,2))
for be in range(2):
    for ga in range(2):
        for al in range(2):
            for alp in range(2):
                for alpp in range(res(al,alp),prod(al,alp)):
                    tensa[be,ga] += tensb[al] * tensc[alp,be] * tensd[alpp,ga] …
Run Code Online (Sandbox Code Playgroud)

python numpy linear-algebra

2
推荐指数
1
解决办法
1240
查看次数

获取 np.linalg.svd 的奇异值作为矩阵

给定一个 5x4 矩阵 A =

\n\n

在此输入图像描述

\n\n

一段构造矩阵的Python代码

\n\n
A = np.array([[1, 0, 0, 0],\n              [0, 0, 0, 4],\n              [0, 3, 0, 0],\n              [0, 0, 0, 0],\n              [2, 0, 0, 0]])\n
Run Code Online (Sandbox Code Playgroud)\n\n

Wolframalpha给出 svd 结果

\n\n

在此输入图像描述

\n\n

具有奇异值 \xce\xa3 的向量采用这种形式

\n\n

在此输入图像描述

\n\n

的输出中的等价数量(NumPy 称之为 s)np.linalg.svd是这种形式

\n\n
[ 4.          3.          2.23606798 -0.        ]\n
Run Code Online (Sandbox Code Playgroud)\n\n

有没有办法让 numpy.linalg.svd 的输出数量显示为 Wolframalpha?

\n

python numpy linear-algebra

2
推荐指数
1
解决办法
5130
查看次数