Consider this bit of code:
#include <iostream>
#include <string>
int main()
{
std::string str;
std::cout << "Enter a string: \n";
getline(std::cin, str);
}
Run Code Online (Sandbox Code Playgroud)
Why must I use std:: for string, cin and cout, but not getline()? Is getline() not in the standard library? I'm actually somewhat confused why I can't just write using namespace std; and not have to #include anything in the standard library, too. Thanks in advance!
考虑Series obj:
In [50]: obj = Series(np.arange(6,10), index = ['a', 'b', 'c', 'd'])
In [51]: obj
Out[51]:
a 6
b 7
c 8
d 9
dtype: int64
Run Code Online (Sandbox Code Playgroud)
我想切一块obj,我可以通过以下几种方式做到这一点:
In [52]: obj[1:3]
Out[52]:
b 7
c 8
dtype: int64
In [53]: obj['b' : 'c']
Out[53]:
b 7
c 8
dtype: int64
Run Code Online (Sandbox Code Playgroud)
现在考虑DataFrame param_estimates_good:
In [54]: param_estimates_good
Out[54]:
a b sigma a_se b_se sigma_se success
1968 0.648508 1.803889 0.498017 0.784340 0.082366 0.529649 1
1972 0.539485 1.733304 …Run Code Online (Sandbox Code Playgroud) 考虑一下这段代码:
#include <iostream>
#include <complex>
int main()
{
std::complex<double> z1 = 5;
std::cout << z1 - 1 << "\n"; // must change to z1 - 1.0 to compile
std::complex<int> z2 = 5;
std::cout << z2 - 1.0 << "\n"; // must change to z2 - 1 to compile
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译错误,因为operator-表达式中的类型没有找到z1 - 1或z2 - 1.0.另一方面,更改这些表达式以使基本类型匹配工作正常.
天真,对z1 - 1我期望int1被提升到double和预期的z2,与基本类型int,在z2 - 1.0晋升为一个complex<double>.这是怎么回事?
考虑两个ndarrays长度n,arr1和arr2.我正在计算以下产品总数,并按时num_runs进行基准测试:
import numpy as np
import time
num_runs = 1000
n = 100
arr1 = np.random.rand(n)
arr2 = np.random.rand(n)
start_comp = time.clock()
for r in xrange(num_runs):
sum_prods = np.sum( [arr1[i]*arr2[j] for i in xrange(n)
for j in xrange(i+1, n)] )
print "total time for comprehension = ", time.clock() - start_comp
start_loop = time.clock()
for r in xrange(num_runs):
sum_prod = 0.0
for i in xrange(n):
for j in xrange(i+1, n):
sum_prod …Run Code Online (Sandbox Code Playgroud) 我有点惊讶的是,该hashCode()方法的默认(本机)实现比以下基准测试的方法的简单覆盖慢了大约 50 倍。
考虑一个Book不覆盖的基本类hashCode():
public class Book {
private int id;
private String title;
private String author;
private Double price;
public Book(int id, String title, String author, Double price) {
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,考虑一个完全相同的Book类,BookWithHash,它hashCode()使用 Intellij 的默认实现覆盖该方法:
public class BookWithHash {
private int id;
private String title;
private String author;
private Double price;
public …Run Code Online (Sandbox Code Playgroud) 我希望向量迭代器指向一个向量元素.我有
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec = {1,2,3,4,5};
std::vector<int>::iterator it;
// want "it" to point to the "3" element, so something like
// it = &prices[2];
// it = &prices.at(2);
}
Run Code Online (Sandbox Code Playgroud)
但这些尝试都不起作用.我想我需要一些返回迭代器的向量函数,而不是地址(?)
在Windows上启动Python 2.7解释器后的第一行:
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
Run Code Online (Sandbox Code Playgroud)
输入dir()命令后,_应定义特殊变量:
>>> _
['__builtins__', '__doc__', '__name__', '__package__']
Run Code Online (Sandbox Code Playgroud)
但是,即使在输入之后_,当我尝试使用以下内容列出交互式命名空间中的所有名称时,它也不会显示dir():
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
Run Code Online (Sandbox Code Playgroud)
如果解释器不在解释器的命名空间中,解释器如何识别它?
我认为!在IPython中添加一个shell命令会使系统shell实际执行命令,但似乎并非如此.考虑以下内容,我开始使用/home/Documents/Rx,启动IPython,使用命令cd(没有!),然后退出IPython并查看我所在的目录:
$ pwd
/home/Documents/Rx
$ ipython
Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Dec 6 2015, 18:08:32)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.3 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: cd Papers/
/home/Documents/Rx/Papers
In [2]: pwd
Out[2]: u'/home/Documents/Rx/Papers'
In [3]: exit() …Run Code Online (Sandbox Code Playgroud) 我意识到有些方法应该用 来调用(),而另一些则不能。我如何使用 IPython 检查是否使用括号?例如下面的文件scratch.py
import numpy as np
arr = np.random.randn(5)
print arr.sort, "\n"
print arr.sort(), "\n";
print arr.shape, "\n";
print arr.shape(), "\n";
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
<built-in method sort of numpy.ndarray object at 0x7fb4b5312300>
None
(5,)
Traceback (most recent call last):
File "scratch.py", line 8, in <module>
print arr.shape(), "\n";
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud)