小编Sam*_*ley的帖子

为什么`True是False == False`,在Python中是假的?

为什么在使用括号时这些语句按预期工作:

>>> (True is False) == False
True

>>> True is (False == False)
True
Run Code Online (Sandbox Code Playgroud)

但是False当没有括号时它会返回?

>>> True is False == False
False
Run Code Online (Sandbox Code Playgroud)

python python-3.x

29
推荐指数
3
解决办法
2705
查看次数

Java条件赋值中的类型错误

在下面的代码中,我有两个相同的条件赋值操作,一个返回Double类型的对象,第二个返回字符串"Integer".

double d = 24.0;

Number o = (d % 1 == 0) ? new Double(d).intValue() : new Double(d).doubleValue();
String result = (d % 1 == 0) ? "Integer" : "Double";

System.out.println(o.getClass()); // prints "class java.lang.Double"
System.out.println(result); // Integer
Run Code Online (Sandbox Code Playgroud)

为什么完全相同的表达式返回两个不同的东西?

java conditional-operator

20
推荐指数
1
解决办法
580
查看次数

MATLAB基于长度向量重复数字

是否有矢量化方式来执行以下操作?(以示例显示):

input_lengths = [ 1 1 1 4       3     2   1 ]
result =        [ 1 2 3 4 4 4 4 5 5 5 6 6 7 ]
Run Code Online (Sandbox Code Playgroud)

我已经间隔了input_lengths,因此很容易理解如何获得结果

合成矢量的长度为:sum(lengths).我目前result使用以下循环计算:

result = ones(1, sum(input_lengths ));
counter = 1;
for i = 1:length(input_lengths)
    start_index = counter;
    end_index = counter + input_lengths (i) - 1;

    result(start_index:end_index) = i;
    counter = end_index + 1;
end
Run Code Online (Sandbox Code Playgroud)

编辑:

我也可以使用arrayfun(虽然这不是一个矢量化函数)

cell_result = arrayfun(@(x) repmat(x, 1, input_lengths(x)), 1:length(input_lengths), 'UniformOutput', false);
cell_result : …
Run Code Online (Sandbox Code Playgroud)

matlab vectorization octave run-length-encoding

11
推荐指数
6
解决办法
533
查看次数

优化MATLAB代码指南

我已经注意到很多关于SO的问题,但没有一个很好的MATLAB优化指南.

常见问题:

  • 为我优化此代码
  • 我该如何对此进行矢量化?

我不认为这些问题会停止,但我希望这里提出的想法能让他们集中参考.

优化Matlab代码是一种黑色艺术,总有一种更好的方法.有时甚至无法对代码进行矢量化.

所以我的问题是:当矢量化不可能或极其复杂时,优化MATLAB代码的一些技巧和窍门是什么?此外,如果你有任何常见的矢量化技巧,我也不介意看到它们.

optimization matlab

8
推荐指数
1
解决办法
3721
查看次数

Pandas DataFrame列连接

我有一个pandas Dataframe y,有100万行和5列.

np.shape(y)  
(1037889, 5)
Run Code Online (Sandbox Code Playgroud)

列值都是0或1.看起来像这样:

y.head()  
a, b, c, d, e  
0, 0, 1, 0, 0  
1, 0, 0, 1, 1  
0, 1, 1, 1, 1  
0, 0, 0, 0, 0
Run Code Online (Sandbox Code Playgroud)

我想要一个包含100万行和1列的Dataframe.

np.shape(y)  
(1037889, )
Run Code Online (Sandbox Code Playgroud)

列只是连接在一起的5列.

New column  
0, 0, 1, 0, 0  
1, 0, 0, 1, 1  
0, 1, 1, 1, 1  
0, 0, 0, 0, 0
Run Code Online (Sandbox Code Playgroud)

我一直在尝试不同的事物一样merge,concat,dstack,等...但似乎无法弄清楚这一点.

python merge numpy concatenation pandas

7
推荐指数
1
解决办法
4537
查看次数

比MATLAB的`system`命令更安全的替代方案

我一直在使用MATLAB的system命令来获取一些linux命令的结果,如下面的简单示例所示:

[junk, result] = system('find ~/ -type f')
Run Code Online (Sandbox Code Playgroud)

除非用户同时键入MATLAB的命令窗口,否则这将按预期工作.在长期find指挥期间这并不罕见.如果发生这种情况,那么用户的输入似乎与find命令的结果混淆(然后事情就会中断).

作为一个例子,而不是:

/path/to/file/one
/path/to/file/two
/path/to/file/three
/path/to/file/four
Run Code Online (Sandbox Code Playgroud)

我可能会得到:

J/path/to/file/one
u/path/to/file/two
n/path/to/file/three
k/path/to/file/four
Run Code Online (Sandbox Code Playgroud)

为了便于演示,我们可以运行如下:

[junk, result] = system('cat')
Run Code Online (Sandbox Code Playgroud)

在命令窗口中键入内容,然后按CTRL + D关闭流.该result变量将是你输入任何在命令窗口.

有没有更安全的方法让我从MATLAB调用系统命令而不会有输入损坏的风险?

linux matlab

7
推荐指数
1
解决办法
968
查看次数

JS/JQuery重试1秒后加载Img

我有一个请求调用一堆图像,如下所示:

<a href='www.domain1.com'><img src='../image/img1.png' onerror='imgError(this);'/></a>
<a href='www.domain2.com'><img src='../image/img2.png' onerror='imgError(this);'/></a>
Run Code Online (Sandbox Code Playgroud)

问题是当进行调用时,一些图像(~20%)还没有准备好.他们需要另一秒钟.

所以在js或jquery中我想做的是在出错时获取失败的图像,等待1秒,然后再尝试加载那些失败的图像.如果他们在第二次尝试失败 - 哦,我很好.但我没有正确地这样做...我不应该在js中的另一个方法中调用超时吗?

function imgError(image) {
    image.onerror = "";
    image.src = image;

    setTimeout(function () {
        return true;
    }, 1000);
}
Run Code Online (Sandbox Code Playgroud)

javascript jquery

6
推荐指数
1
解决办法
2796
查看次数

如何检查迭代器是否实际上是迭代器容器?

我有一个下面的迭代器容器的虚拟示例(真正的读取文件太大而不适合内存):

class DummyIterator:
    def __init__(self, max_value):
        self.max_value = max_value

    def __iter__(self):
        for i in range(self.max_value):
            yield i

def regular_dummy_iterator(max_value):
    for i in range(max_value):
        yield i
Run Code Online (Sandbox Code Playgroud)

这允许我多次迭代该值,以便我可以实现这样的事情:

def normalise(data):
    total = sum(i for i in data)
    for val in data:
        yield val / total

# this works when I call next()
normalise(DummyIterator(100))

# this doesn't work when I call next()
normalise(regular_dummy_iterator(100))
Run Code Online (Sandbox Code Playgroud)

如何检查normalize函数,我正在传递一个迭代器容器而不是一个普通的生成器?

python generator python-3.x

6
推荐指数
1
解决办法
385
查看次数

Mac地址上的Javascript正则表达式

假设我有一个mac地址列表,例如:"00:11:22:33:44:55,11:22:33:44:55:66,22:33:44:55:66:77"

我想对该列表进行正则表达式检查.

var re = /(([A-Fa-f0-9]{2}[:]){5}[A-Fa-f0-9]{2}[,]?)+/g
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用.这是输入测试.

var t1 = "11:22:33:44:55:66";
var t2 = t1 + ",12:22:33:44:55:66";
var t3 = t1 + ",11asdfasdf:22:33:44:55:66";
var t4 = t1 + ",haha";
var t5 = t1 + ",";
var t6 = "123123123123";
var t7 = t1 + ",33:44:55:66:77:88:";
var t8 = t1 + ",33:44:55:66:77:88asdfasdfasdfasdfasdfasd";
var t9 = t1 + ",dfasdfasdfasdfasdfasd";
var t10 = t2 + ",12:33:44:55:66:77";
var t11 = t2 + ",wahaa";

console.log("t1: [" + t1 + "] " + re.test(t1));
console.log("t2: [" …
Run Code Online (Sandbox Code Playgroud)

javascript regex mac-address

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

405 error{"Message":"The requested resource does not support http method 'GET'."}

I am using the following code in my interface and using webapi service.

[OperationContract]
[WebInvoke(Method = "GET")]
bool IsPaymentGatewayExitsForTenant(string tenantSlugName, string productCode);
Run Code Online (Sandbox Code Playgroud)

I am running locally and testing the service using rest client

api/PaymentGatewayService/IsPaymentGatewayExitsForTenant?tenantSlugName=KPN&productCode=POSS
Run Code Online (Sandbox Code Playgroud)

But I am getting the following errror enter code here

{"Message":"The requested resource does not support http method 'GET'."} `enter code here`
Run Code Online (Sandbox Code Playgroud)

Any help would be appeciated.

asp.net-web-api

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

如何确定字符串中的数字类型?

我们有这些字符串:

  • "1/2"
  • "1"
  • "0.9"
  • "B"
  • "0,9"

如何确定字符串的数据类型?

  • 有理数
  • 整数
  • 浮动
  • 不是数字
  • 不是数字

在一段代码?

ruby validation numeric

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