我有一个关于搜索算法的问题.我目前有2个纯文本文件,每个文件至少有1000万行.现在,每一行都是一个字符串,我想找到第一个文件中也出现在第二个文件中的每个字符串.有效地做到这一点有好方法吗?任何来自算法或特殊语言功能的建议都表示赞赏.
当我试图调用"new"来创建一个指针并将其推入向量时,我得到了段错误.我在向量中推送元素的代码是:
queue->push_back(new Box(gen_id, Interval(x_mid, x_end), Interval(y_mid-y_halfwidth, y_mid+y_halfwidth)));
Run Code Online (Sandbox Code Playgroud)
基本上Box是一个类,构造函数只需要3个参数generation_id,和2 Intervals.在此"推送"之前和之后,我在向量中打印出内容,然后:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.30908203125, -0.3087158203125 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
Run Code Online (Sandbox Code Playgroud)
后:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ 8.9039208750109844342e-243, 6.7903818933216500424e-173 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] …Run Code Online (Sandbox Code Playgroud) 我没有那么多使用C,最近我对2d数组初始化问题感到困惑.我需要调试某人的代码并坚持以下(她的原始代码):
const int location_num = 10000;
bool **location_matrix;
if (node_locations)
{
location_matrix = (bool **)malloc(location_num*sizeof(bool *));
if (!location_matrix)
{
cout<<"error 1 allocating location_matrix" << endl;
exit;
}
for (i=0; i<location_num; i++)
{
location_matrix[i] = (bool *) malloc(location_num*sizeof(bool ));
if (!location_matrix[i])
{
cout<<"error 2 allocating location_matrix" << endl;
exit;
}
for (j=0; j<location_num; j++)
location_matrix[i][j] = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为是多余的,所以我将其更改为以下内容:
location_matrix[location_num][location_num] = { {false} };
但是,分段错误在运行时发生.我的问题是:上面的代码是如何失败的?如果它看起来正确,动态分配和静态分配之间有什么区别?是因为维度可能不是常数,所以我们需要动态地做这个吗?另外,只是为了好奇,我如何malloc 2d数组存储指针?谢谢.
我是C++的新手,我正在尝试使用模板,但我遇到了问题.我要做的是:尝试使用模板计算数字的平方,并且数字可以是基本数据类型,如int,float,以及复数.我还使用模板实现了一个复杂的类,代码如下:
template <typename T>
class Complex {
public:
T real_;
T img_;
Complex(T real, T img) : real_(real), img_(img) { }
};
template <typename T>
T square(T num) {
return num * num;
}
template <>
Complex<typename T> square(Complex<typename T> num) {
T temp_real = num.real_*num.real_ - num.img_*num.img_;
T temp_img = 2 * num.img_ * num.real_;
return Complex(temp_real, temp_img);
}
Run Code Online (Sandbox Code Playgroud)
我试图使用模板专门化来处理特殊情况,但它给了我错误:
using ‘typename’ outside of template
Run Code Online (Sandbox Code Playgroud)
并且错误发生在模板特化方法上.请指出我的错误.谢谢.
我从django后端获取ajax发布数据时遇到问题,我不知道如何传递该值,请帮忙.
在HTML中我只是这个:
<form id="get_vulns_from_family">
<label for="family_content">Enter a family name to display the NVTs</label>
<input id="family_content" />
<input type="submit" value="search" />
</form>
Run Code Online (Sandbox Code Playgroud)
在javascript中我写了这个:
$(function() {
$("#get_vulns_from_family").submit(function(event) {
var family_text = $("#family_content").val();
var family_data = {"family": family_text};
$.ajax({
url: "/template_conf/get_vulns_from_family",
type: "POST",
data: family_data,
success: function(response) {
console.log(response);
},
error: function(response) {
console.log("failed!");
}
});
// prevent default posting of form
event.preventDefault();
});
});
Run Code Online (Sandbox Code Playgroud)
在与url对应的Django方法中/template_conf/get_vulns_from_family,我试过这个:
def get_vuln_from_family(request):
family = request.POST['family']
# some other operations to get value for variable "json_data" …Run Code Online (Sandbox Code Playgroud) 我有一个 Django 模型,例如:
class MyModel(models.Model):
no_validation = models.PositiveIntegerField(null=True, db_index=True)
yes_validation = models.CharField(max_length=254, validators=[my_validator])
Run Code Online (Sandbox Code Playgroud)
我还有一个预先保存的钩子,类似于:
@receiver(pre_save, sender=MyModel)
def run_full_clean(sender, instance, **kwargs):
instance.clean_fields()
Run Code Online (Sandbox Code Playgroud)
我的问题是这种当前的方法会清理所有领域。我只想清理一个特定字段(称为 yes_validation 的字段)。有没有简单的方法来做到这一点?
我只知道exclude参数,但问题是我的班级有近一百个不同的字段,因此排除每个字段都会有点脏。
当Entry对象的数量大于5000个条目时,django中有没有办法更有效地执行以下操作?
models.py
class Entry(models.Model):
user = models.TextField(db_column='User', blank=True)
date = models.DateTimeField(blank=True)
class Color(models.Model):
color = models.TextField(blank=True)
entry = models.ForeignKey(Entry)
Run Code Online (Sandbox Code Playgroud)
让我们说我希望获得每个条目的所有颜色......
entrys = Entry.objects.all()
for e in entrys:
print e.color_set.all()
Run Code Online (Sandbox Code Playgroud)
我希望能够将每个对象与特定条目相关联.例如,在像这样的csv表中.
user, color
john, blue
john, orange
bob, green
bob, red
bob, purple
Run Code Online (Sandbox Code Playgroud)
查看我的所有条目需要几秒钟的时间.有没有更好的办法?
我有一个接受时间序列数据的 python 服务器。现在我需要计算最后一分钟的平均流量,输出 90 个样本/分钟。我目前正在使用 python 列表来保存所有时间戳,并使用一种非常糟糕的方式(在我看来)来计算它。代码大致如下:
class TrafficCalculator(object):
timestamps = []
def run():
while True:
# this gets one record of traffic
data = self.accept_data()
# get record's timestamp
timestamp = data.timestamp
# add to list
self.timestamps.append(timestamp)
# get the time one minute ago
minute_ago = timestamp - datetime.timedelta(minutes=1)
# find out the first index of the timestamp in the past that's within 1 minute
for i, t in enumerate(self.timestamp):
if t > minute_ago:
break
# see how many …Run Code Online (Sandbox Code Playgroud) 我正在使用Python调用bash来执行另一个bash脚本:
begin = int(sys.argv[1])
result = os.system("/tesladata/isetools/cdISE.bash %s" %begin)
Run Code Online (Sandbox Code Playgroud)
打印后result,它不仅给我输出,还给出了返回状态(0这里).如果我只需要输出,我该怎么办?
而且,仅仅是为了好奇,有多少方法可以在Python中调用bash?我会很高兴,如果有人可以给我如何使用它们的一些参考,我只发现os.system()和os.popen()至今.
我在石墨日志中看到很多这样的线条:
01/10/2014 21:07:12 :: [listener] invalid line received from client HOST:PORT, ignoring
Run Code Online (Sandbox Code Playgroud)
如果我能看到无效的行,那将会非常有用.一些文档和教程建议石墨会在无效警告后直接打印违规行,但对我来说却没有.我该如何启用此属性?
谢谢.
python ×4
c++ ×3
django ×3
algorithm ×2
ajax ×1
allocation ×1
bash ×1
call ×1
graphite ×1
logging ×1
matching ×1
new-operator ×1
optimization ×1
post ×1
search ×1
string ×1
templates ×1
validation ×1