小编Vid*_*dak的帖子

iter()不使用datetime.now()

Python 3.6.1中的一个简单片段:

import datetime
j = iter(datetime.datetime.now, None)
next(j)
Run Code Online (Sandbox Code Playgroud)

收益:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
Run Code Online (Sandbox Code Playgroud)

而不是打印now()每个经典行为next().

我已经看到类似的代码在Python 3.3中工作,我在版本3.6.1中遗漏了什么或者有什么变化?

python datetime iterable python-3.6

44
推荐指数
2
解决办法
1510
查看次数

Numpy串联+合并一维数组

我需要连接数组,但如果它们重叠,还需要将A的结尾与B的开头合并。

[1, 2, 4] + [2, 4, 5] -> [1, 2, 4, 5]
[1, 2, 4] + [2, 5, 4] -> [1, 2, 4, 2, 5, 4]
[1, 2, 4] + [1, 2, 4, 5] -> [1, 2, 4, 5]
Run Code Online (Sandbox Code Playgroud)

注意:必须保留元素的顺序,[4,5]与[5,4]不同。

注2:也可以这样理解问题:我们需要A的最短扩展,以便输出以B结尾。

当然,我可以遍历第二个数组并逐个元素进行比较,但是我正在寻找一个不错的Numpy解决方案。

python arrays performance numpy

6
推荐指数
1
解决办法
89
查看次数

从复合图案结构中删除元素

假设我们有最简单的结构,类Component,Leaf : ComponentComposite : Component.Leaf类的每个对象都有一个int id赋予它标识的对象.复合类会像:

class Composite : public Component
{
public:
    void removeComponent(Component*);
// other stuff
private:
    std::vector<Component*> coll;
};
Run Code Online (Sandbox Code Playgroud)

和叶班一样:

class Leaf : public Component
{
public:
    //stuff
    int getID();
private:
    int id;
};
Run Code Online (Sandbox Code Playgroud)

问题是如何定义功能removeComponent(Component* cmp).cmp实际上是一个Leaf,但我需要访问Component矢量coll,所以它需要是一个Component(我认为).removeComponent方法接受一个Leaf对象,并从整个结构中删除具有相同ID的所有其他Leaves.

现在,我想到了两种方式(两者都不起作用:P):

第一

void Composide::removeComponent(Component* cmp)
{
    std::vector<Component*>::const_iterator it;
    for(it = coll.begin(); it != coll.end(); ++it)
    {
        (*it)->removeComponent(cmp);
    // where removeComponent is …
Run Code Online (Sandbox Code Playgroud)

c++ oop algorithm inheritance design-patterns

5
推荐指数
1
解决办法
1273
查看次数

用于创建索引的Django"NULLS LAST"

Django 1.11及更高版本允许使用F-expressions为nulls last查询添加选项:

queryset = Person.objects.all().order_by(F('wealth').desc(nulls_last=True))
Run Code Online (Sandbox Code Playgroud)

但是,我们希望使用此功能来创建索引.模型定义中的标准Django索引:

indexes = [
        models.Index(fields=['-wealth']),
]
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方面:

indexes = [
        models.Index(fields=[models.F('wealth').desc(nulls_last=True)]),
]
Run Code Online (Sandbox Code Playgroud)

返回AttributeError: 'OrderBy' object has no attribute 'startswith'.

这可以F在Django中使用-expressions吗?

python django indexing null django-models

5
推荐指数
1
解决办法
308
查看次数

postfix前缀运算符重载错误c ++

当使用运算符重载前缀和后缀增量时,我从编译器收到错误:

"Fajl Fajl :: operator ++(int)':已定义或声明的成员函数"

以下是operator ++的标题:

Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
Run Code Online (Sandbox Code Playgroud)

而我的实施:

Fajl& Fajl::operator ++ () // prefix
{
    ++(*poz);
    return *this;
}

Fajl& Fajl::operator -- () // prefix
{
    --(*poz);
    return *this;
}

Fajl Fajl::operator ++ (int dummy) // postfix
{
    Fajl temp(*this);
    ++(*this);
    return temp;
}

Fajl Fajl::operator -- (int dummy) // postfix
{
    Fajl temp(*this);
    --(*this); …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading prefix-operator postfix-operator operator-keyword

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

删除此私有析构函数

我一直在考虑delete this在c ++中可能的用法,我看过一次使用.

因为你delete this只能在一个对象在堆上时说,我可以使析构函数变为私有,并完全阻止在堆栈上创建对象.最后,我可以通过delete this在充当析构函数的随机公共成员函数中说明来删除堆上的对象.我的问题:

1)为什么我要强制在堆上而不是在堆栈上创建对象?

2)除此之外还有其他用途delete this吗?(假设这是合法使用它:))

c++ memory oop destructor this

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

可空列的Django REST ViewSet排序

考虑一个表人:

| name | wealth |
|------| ------ |
|Abby  | 12     |
|Ben   | Null   |
|Carl  | 5      |
|Diane | Null   |
Run Code Online (Sandbox Code Playgroud)

我们想按财富降序对行进行排序,即得到get (Abby, Carl, Ben, Diane),但是Django的order_by函数首先按Null 对行进行排序:

class PersonViewSet(serializers.ModelViewSet):
        serializer_class = PersonSerializer
        queryset = Person.objects.all().order_by('-wealth)
Run Code Online (Sandbox Code Playgroud)

(Ben, Diane, Abby, Carl),即它首先列出Null值,然后按财富排序。


我尝试重新定义get_queryset方法:

class PersonViewSet(serializers.ModelViewSet):
    serializer_class = PersonSerializer

    def get_queryset():
        invalid_entries = Person.objects.filter(wealth=None)
        valid_entries = Person.objects.all().difference(invalid_entries).order_by('-wealth')
        return valid_entries.union(invalid_entries)
Run Code Online (Sandbox Code Playgroud)

这确实返回了所需的行为,(Abby, Carl, Ben, Diane)但搞砸了详细视图并给出了get() returned multiple values错误。


是否可以通过自定义排序功能或get_queryset 仅 …

python django serialization django-rest-framework django-rest-viewsets

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

函数返回临时对象行为不端

好的,非常简单的String类,它保存常量字符串(即初始化后不能更改),实现复制ctor和连接功能conc.这个函数给了我麻烦,因为我真的无法弄清楚为什么我做的局部变量不能正常传递为返回值.

码:

class String
{
public:
    String(char*);
    String(const String& other);
    ~String();
    friend String conc(const String&, const String&);
    friend std::ostream& operator<<(std::ostream&, const String&);
private:
    const char* const str;
};

String::String(char* aStr) : str(aStr) {}

String::String(const String& other) : str(other.str) {}

std::ostream& operator<<(std::ostream& out, const String& s)
{
    out<<s.str<<"\n";
    return out;
}

String::~String()
{
    delete str;
}

String conc(const String& s1, const String& s2)
{
    int n = strlen(s1.str) + strlen(s2.str);
    int i, j; …
Run Code Online (Sandbox Code Playgroud)

c++ oop string function return-value

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

析构函数和类属性的指针

我有这个类ChessBoard,这是它的标题:

class ChessBoard
{
    Field** board;
    Color currentColor; 

public:
    ChessBoard();
    ChessBoard(const ChessBoard&);
    Field* findField(std::string);
    ChessBoard& operator = (const ChessBoard&);

    bool checkIfFieldHasFigure(std::string);


    void writeOneState(SahApi*);
    void playAllMoves(std::istream*, SahApi*);
    void playMove(std::string);
    ~ChessBoard();

};
Run Code Online (Sandbox Code Playgroud)

我有它的构造函数,它创建了棋盘的开始阶段:(非常可怕,我知道:))

ChessBoard::ChessBoard()
{
    int i, j;
    Error err;
    board = new Field*[8];
    if(!board)
    {
        err.writeToOutput(1);
        exit(1);
    }
    for(i = 0; i < 8; i++)
    {
        board[i] = new Field[8];
        if(!board[i])
        {
            err.writeToOutput(1);
            exit(1);
        }
    }
    currentColor = WHITE;
    char c;
    std::string s;
    Figure f;
    for(i = 0; i < 8; …
Run Code Online (Sandbox Code Playgroud)

c++ constructor pointers destructor memory-management

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

如何使用自定义值定义类型?(typedef,enum)

在我的许多类的程序中,我使用它Color作为一个类型,它应该只有WHITEBLACK作为它的可能值.

所以我想写一下:

Color c; 
  c = BLACK;
  if(c == WHITE) std::cout<<"blah";
Run Code Online (Sandbox Code Playgroud)

和类似的东西.在我所说的所有类和标题中#include "ColorType.h",我都有Color c类属性,但我不知道该写些什么ColorType.h.我尝试了一些变化,typedef enum Color但它没有完全解决.

c++ enums types

-1
推荐指数
1
解决办法
1538
查看次数