MyClass a1 {a}; // clearer and less error-prone than the other three
MyClass a2 = {a};
MyClass a3 = a;
MyClass a4(a);
Run Code Online (Sandbox Code Playgroud)
为什么?
我在SO上找不到答案,所以让我回答一下我自己的问题.
我有这样的字典:
sample = {'ObjectInterpolator': 1629, 'PointInterpolator': 1675, 'RectangleInterpolator': 2042}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何将dict转储到json
文件中,如下所示:
{
"name": "interpolator",
"children": [
{"name": "ObjectInterpolator", "size": 1629},
{"name": "PointInterpolator", "size": 1675},
{"name": "RectangleInterpolator", "size": 2042}
]
}
Run Code Online (Sandbox Code Playgroud)
有没有pythonic方式来做到这一点?
您可能猜到我想生成一个d3
树形图.
在C中,我有两个char数组:
char array1[18] = "abcdefg";
char array2[18];
Run Code Online (Sandbox Code Playgroud)
如何复制的价值array1
来array2
?我可以这样做array2 = array1
吗?
我试图使用GCC(测试版本4.5.1,4.6.3,4.8.4)编译此示例程序:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
using std::chrono::system_clock;
int main()
{
system_clock::time_point now = system_clock::now();
std::time_t now_c = system_clock::to_time_t(
now - std::chrono::hours(24));
std::cout << "One day ago, the time was "
<< std::put_time(std::localtime(&now_c), "%F %T") << '\n';
}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In function 'int main()':
prog.cpp:14:18: error: 'put_time' is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)
我想,可能还没有实现.所以我试着检查这个功能的实现状态.我只找到了这个页面:
但我找不到任何笔记put_time
或chrono
或相似.任何人都可以指向我提供有关此库的实现状态的信息的资源吗?
我正在尝试着色使用创建的Voronoi图scipy.spatial.Voronoi
.这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d
# make up data points
points = np.random.rand(15,2)
# compute Voronoi tesselation
vor = Voronoi(points)
# plot
voronoi_plot_2d(vor)
# colorize
for region in vor.regions:
if not -1 in region:
polygon = [vor.vertices[i] for i in region]
plt.fill(*zip(*polygon))
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果图像:
正如您所看到的,图像边界处的一些Voronoi区域没有着色.这是因为这些区域的Voronoi顶点的一些索引被设置为-1
,即,对于Voronoi图之外的那些顶点.根据文件:
区域:(整数列表,形状(nregions,*))形成每个Voronoi区域的Voronoi顶点的索引.-1表示Voronoi图外的顶点.
为了使这些区域着色,我试图从多边形中删除这些"外部"顶点,但这不起作用.我想,我需要在图像区域的边界填写一些点,但我似乎无法弄清楚如何合理地实现这一点.
有人可以帮忙吗?
我有一个df
像以下一样的DataFrame (摘录,'Timestamp'是索引):
Timestamp Value
2012-06-01 00:00:00 100
2012-06-01 00:15:00 150
2012-06-01 00:30:00 120
2012-06-01 01:00:00 220
2012-06-01 01:15:00 80
...and so on.
Run Code Online (Sandbox Code Playgroud)
我需要一个新列df['weekday']
,其中包含时间戳的相应工作日/星期几.
我怎么能得到这个?
我从epoch开始有以下时间戳:
Timestamp
1346114717972
1354087827000
Run Code Online (Sandbox Code Playgroud)
如何将这些时间戳转换为某种特定的输出格式,例如mm/dd/yyyy hr:min:sec
?
我试图将它们转换为datetime.datetime
但失败了:
>>> datetime.datetime.fromtimestamp(1346114717972)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: timestamp out of range for platform time_t
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
在Python 3中,我可以执行以下操作(另请参阅PEP3132 on Extended Iterable Unpacking):
a, *b = (1, 2, 3)
# a = 1; b = (2, 3)
Run Code Online (Sandbox Code Playgroud)
我能做些什么才能在Python 2.x中实现同样优雅?
我知道我可以使用单元素访问和切片操作,但我想知道是否有更多的pythonic方式.我的代码到目前为止:
a, b = (1, 2, 3)[0], (1, 2, 3)[1:]
# a = 1; b = (2, 3)
Run Code Online (Sandbox Code Playgroud) python ×5
c++ ×3
c ×2
c++11 ×2
arrays ×1
c++-chrono ×1
char ×1
copy ×1
dictionary ×1
gcc ×1
header-only ×1
json ×1
matplotlib ×1
pandas ×1
scipy ×1
std ×1
syntax ×1
voronoi ×1