小编Jec*_*cke的帖子

在numpy,Python中,当我想要设置的值是在不同大小的数组中时,如何有条件地重写数组的一部分?

假设我有三个数组:

A[size1] of {0..size1}
B[size2] of {0..size1}
C[size2] of boolean
Run Code Online (Sandbox Code Playgroud)

我想要的是:

for (int e = 0; e < size2; ++e) :
    if C[e] == some_condition, then B[e] = A[B[e]]
Run Code Online (Sandbox Code Playgroud)

由于Python很慢,我必须通过数组上的numpy算法来实现它.我怎样才能做到这一点?

例:

A = np.array([np.random.randint(0,n,size1), np.random.randint(0,size1,size1)])
B = np.random.randint(0,size1,size2)
C = np.random.randint(0,n,size2)

#that's the part I want to do in numpy:
for i in range (size2) :
    if (C[i] > A[0][B[i]]) : 
        B[i] = A[1][B[i]]
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

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

为什么我无法重新分配该字符串?

我有一个这样的结构:

typedef struct TEXT {
    char *text;
    struct TEXT *next;
} TEXT;
Run Code Online (Sandbox Code Playgroud)

在某些功能中我有类似的东西:

TEXT *line = (TEXT *) malloc(sizeof(TEXT));
line->text = NULL; // was "\0" before
char current = getchar();
while (current != '\n') {
    AddChar(&(line->text), current); // Was AddChar(line->text, current);
    current = getchar();
}
Run Code Online (Sandbox Code Playgroud)

AddChar 函数是这样的:

void AddChar(char **text, char c) { //was char *text
    *text = (char *) realloc(*text, strlen(*text)+2); //was text = ...
    char char_array[2] = {c, '\0'); 
    strcat(*text, char_array); //was strcat(text, char_array);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,程序崩溃了。

据我了解,事实证明 strlen …

c string struct pointers null-terminated

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

如何在 django 中测试使用 post 请求的视图?

这是我的观点(简化):

@login_required(login_url='/try_again')
def change_bar(request):
    foo_id = request.POST['fid']
    bar_id = request.POST['bid']
    foo = models.Foo.objects.get(id=foo_id)
    if foo.value > 42:
            bar = models.Bar.objects.get(id=bar_id)
            bar.value = foo.value
            bar.save()
    return other_view(request)
Run Code Online (Sandbox Code Playgroud)

现在我想检查此视图是否正常工作(在这个简化的模型中,Bar 实例是否在应该时更改值)。我该怎么办?

django http-post django-models django-views django-testing

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

尝试将单词插入trie时出现分段错误

嗨:)有谁能告诉我为什么以下代码不起作用?程序if(children[word[letter_no] - 'A'] == nullptr)在对应的节点中的行崩溃'B'.但节点创建,当我尝试调用children[1]构造函数,它的工作原理.但是当它在insert()函数中调用时,它不会......

包括

#include <memory> //shared_ptr
#include <string>    
using namespace std;    
const int ALPHABET = 26;

class Node {
public:
    shared_ptr<Node> children[ALPHABET];

    Node() { for (int i = 0; i < ALPHABET; ++i) children[i] = nullptr;}
    void insert(const string &word, unsigned letter_no) {
        if (letter_no < word.length()) {
            if (children[word[letter_no] - 'A'] == nullptr) 
                children[word[letter_no] - 'A'] = make_shared<Node>();
            children[word[letter_no] - 'A']->insert(word, letter_no+1);
        }
    }
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ pointers trie

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