我想弄清楚d3.js. 在定义轴时,如何在x轴上获得自定义标签.例如,我得到的默认标签是:
|------|------|------|------|------|------|
20 30 40 50 60 70 80
Run Code Online (Sandbox Code Playgroud)
然而,我想要的东西:
|------|------|------|------|------|------| ....
20 26 32 38 44 50 56
Run Code Online (Sandbox Code Playgroud)
我目前正在学习它,并从提供的官方示例中处理代码(略微修改):
var xAxis = d3.svg.axis().scale(x).tickPadding(7).orient("bottom");
var yAxis = d3.svg.axis().scale(y).tickPadding(5).orient("left");
Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么可以通过类实例更改obj的私有值?
考虑以下(部分)代码:
class Group {
private:
int id;
public:
void set_id(int);
int get_id();
bool operator==(const Group&);
};
bool Group::operator==(const Group& g) {
if(g.id == this->id) { /* id is private? */
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
代码编译和结果似乎正确.但是,在if
运算符重载实现的部分中,我们直接访问其参数的私有成员 - const Group& g
但是这样的访问不是无效的吗?
我正在尝试生成图像的颜色直方图.我正在使用PIL读取图像文件,并尝试通过matplotlib绘制相同的图形.
im = Image.open(sys.argv[1])
w, h = im.size
colors = im.getcolors(w*h) #Returns a list [(pixel_count, (R, G, B))]
Run Code Online (Sandbox Code Playgroud)
更新:经过一些试验和错误后,此代码绘制直方图,但不是颜色!(即使对于320x480 jpeg,也需要花费很长时间才能消耗大量的内存)
for idx, c in enumerate(colors):
plt.bar(idx, c[0], color=hexencode(c[1]))
plt.show()
Run Code Online (Sandbox Code Playgroud)
哪里,
def hexencode(rgb):
return '#%02x%02x%02x' % rgb
Run Code Online (Sandbox Code Playgroud)
执行时,程序开始消耗无限的内存,并且不提供显示.OS内存使用率在几分钟内从<380 MB到> 2.5 GB; 我终止执行的帖子.我怎样才能解决问题?
以下是具有显性红色阴影的图像颜色直方图的示例:
我正在尝试使用解析HTML
a = lxml.html.fromstring('<html><body><span class="cut cross">Text of double class</span><span class="cross">Text of single class</span></body></html>')
s1 = a.xpath('.//span[@class="cross"]')
s2 = a.xpath('.//span[@class="cut cross"]')
s3 = a.xpath('.//span[@class="cut"]')
Run Code Online (Sandbox Code Playgroud)
输出:
s1 => [<Element span at 0x7f0a6807a530>]
s2 => [<Element span at 0x7f0a6807a590>]
s3 => []
Run Code Online (Sandbox Code Playgroud)
但是第一个span标签有'cut'类,但是s3是空的.在s2中,当我给两个类时,它返回标记.
在尝试为给定列表生成电源时,我通过互联网遇到了这个功能.没有解释,但测试表明它似乎正常工作.我无法理解这个功能是如何工作的.我会感谢任何这样的解释.
generateSubset [] = [[]]
generateSubset (x:xs) = let p = generateSubset xs in p ++ map (x:) p
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此节拍检测算法在 python 中进行音频处理。我已经实现了上述文章中的第一个(非优化版本)。虽然它打印了一些结果,但我无法检测它是否具有一定的准确性,因为我不知道如何用它播放声音。
目前,我习惯Popen
在进入计算循环之前用歌曲异步启动媒体播放器,但我不确定此策略是否有效并给出同步结果。
#!/usr/bin/python
import scipy.io.wavfile, numpy, sys, subprocess
# Some abstractions for computation
def sumsquared(arr):
sum = 0
for i in arr:
sum = sum + (i[0] * i[0]) + (i[1] * i[1])
return sum
if sys.argv.__len__() < 2:
print 'USAGE: wavdsp <wavfile>'
sys.exit(1)
numpy.set_printoptions(threshold='nan')
rate, data = scipy.io.wavfile.read(sys.argv[1])
# Beat detection algorithm begin
# the algorithm has been implemented as per GameDev Article
# Initialisation
data_len = data.__len__()
idx = 0
hist_last = …
Run Code Online (Sandbox Code Playgroud) 我正在运行以下代码; 它来自Twisted Framework的示例列表,属于Web服务器.
from twisted.web import server, resource
from twisted.internet import reactor
class HelloResource(resource.Resource):
isLeaf = True
numberRequests = 0
def render_GET(self, request):
self.numberRequests += 1
request.setHeader("content-type", "text/plain")
arg = request.args
q = arg['q']
#Added for debugging
for key, value in arg.iteritems():
print key, value
return "I am request #" + str(self.numberRequests) + " " + q[0] + "\n"
reactor.listenTCP(8080, server.Site(HelloResource()))
reactor.run()
Run Code Online (Sandbox Code Playgroud)
执行:
[user@localhost pytwist]$ python twi.py
Run Code Online (Sandbox Code Playgroud)
浏览器网址:
http://localhost:8080/?q=test
Run Code Online (Sandbox Code Playgroud)
但是,我在控制台上收到以下KeyError异常:
File "twi.py", line 12, in render_GET
q = …
Run Code Online (Sandbox Code Playgroud) 这是代码(Euclid的GCD算法).当然有Prelude.gcd
一些练习我正在实施自己的练习.
selfGCD :: Integral f => f -> f -> f
selfGCD a b = if b == 0 then
return a
else
return (selfGCD a (mod a b))
Run Code Online (Sandbox Code Playgroud)
使用ghci,我收到以下错误:
two.hs:32:25:
Couldn't match type `f' with `m0 f'
`f' is a rigid type variable bound by
the type signature for selfGCD :: Integral f => f -> f -> f
at two.hs:31:1
In the return type of a call of `return'
In the expression: return a …
Run Code Online (Sandbox Code Playgroud) 我有一个我正在计算的脚本:
def sumsquared(arr):
sum = 0
idx = 0
len = arr.__len__()
while idx < (len - 1):
sum = sum + (arr[idx] * arr[idx]) + (arr[idx+1] * arr[idx+1])
idx = idx + 2
return sum
Run Code Online (Sandbox Code Playgroud)
上面的函数在循环中调用,循环填充两个列表并调用此函数两次:第一次使用len~1024项目列表,第二次使用len~44100项目.根据输入,循环本身可以运行100到100000次.
对于小尺寸输入,cProfile
基于概要分析通知我:
ncalls tottime percall cumtime percall filename:lineno(function)
---------------------------------------------------------------------
2560 12.065 0.005 12.065 0.005 beat.py:8(sumsquared)
Run Code Online (Sandbox Code Playgroud)
这大约是脚本总运行时间的95%.有什么方法可以加快功能吗?
考虑以下测试程序(键盘执行):
#include <stdio.h>
#include <string.h>
struct camp {
char b[8];
};
int main()
{
struct camp c;
strcpy(c.b, "Hello");
c.b[5] = '\0';
printf("c size: %d\nAddress (amp): %d :: %d\n", sizeof(c), &c, c);
printf("Address of b: %d :: %d\n", &(c.b), c.b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
c size: 8
Address (amp): -1082463628 :: 1819043144
Address of b: -1082463628 :: -1082463628
Run Code Online (Sandbox Code Playgroud)
由(&(c.b)
和c.b
第二次调用printf)给出的地址相同,struct camp c
(第一次调用printf)返回不同的地址.此外,&c
与&(c.b)
或相同c.b
.
究竟发生了什么?
python ×5
haskell ×2
c ×1
c++ ×1
d3.js ×1
image ×1
lxml ×1
matplotlib ×1
optimization ×1
twisted ×1