小编ruo*_*ola的帖子

为什么用 C++ std::array 创建二维数组需要一对额外的 {}

要使用 std::vector 创建二维数组,您可以

vector<vector<int>> array2d = {{1, 2,  3,  4},
                               {5, 6,  7,  8},
                               {9, 10, 11, 12}};
Run Code Online (Sandbox Code Playgroud)

外层 {} 代表外层向量;内部 {},内部向量。

但是,要使用 std::array 创建二维数组,您需要执行以下操作

array<array<int,4>, 3> array2d = {{{1, 2,  3,  4},
                                   {5, 6,  7,  8},
                                   {9, 10, 11, 12}}};
Run Code Online (Sandbox Code Playgroud)

为什么 std::array 的 std::array 需要一对额外的封闭 {}?

c++ stdarray

14
推荐指数
0
解决办法
120
查看次数

Python 以什么顺序查找语法错误?

致力于为学生创建语法调试练习。我们有以下例子。

def five():
    print('five')
return 5

def hello();
   print('hello')
Run Code Online (Sandbox Code Playgroud)

但是在运行文件时,语法错误是

def hello();
           ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我已经看遍了,但无法弄清楚为什么编译器 [原文如此] 不抱怨函数外部的 return 关键字,而是首先找到它下面的分号错误。

Python以什么顺序检查文件语法?这是规范的一部分还是实现定义?

python syntax parsing syntax-error python-3.x

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

Namecheap 域不会在没有“www”的情况下重定向

我希望将我的域连接到 heroku 应用程序。到目前为止,我读过的教程说我们希望 Namecheap 具有以下域记录example.com

CNAME Record    www    www.example.com.herokudns.com
URL Redirect    @      https://www.example.com
Run Code Online (Sandbox Code Playgroud)

使用此配置,我可以使用以下方法成功访问我的主页:

但由于某种原因https://example.com/无法连接并超时。有谁知道是否有办法让https://example.com/https://www.example.com/成功重定向?

dns ssl https heroku namecheap

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

在与否定的正则表达式匹配的字符串中找到最后位置的Python方法

在Python中,我尝试在与给定模式匹配的任意字符串中找到最后一个位置,该模式指定为负字符集正则表达式模式。例如,使用字符串uiae1iuae200,并且模式不是数字(Python中的正则表达式模式为[^0-9]),结果将需要'8'(在'200'之前的最后一个'e')。

实现这一目标的最有效方法是什么?

由于这是一个有点棘手迅速找到方法的文档,并在Python文档(由于方法的文档是在相应页面的中间位置,喜欢的东西最适合的方法re.search()重新页),最好的办法我很快发现自己正在使用re.search()-但当前表单必须只是次优的方式:

import re
string = 'uiae1iuae200' # the string to investigate
len(string) - re.search(r'[^0-9]', string[::-1]).start()
Run Code Online (Sandbox Code Playgroud)

我对此不满意,原因有两个:-a)我需要string先将其反转,然后再将其与[::-1]-b)我还需要反转结果位置(将其减去是len(string)因为之前已经反转了字符串)。

即使有的结果,也需要有更好的方法re.search()

我知道re.search(...).end()over .start(),但是re.search()似乎将结果分成几组,为此,我没有很快找到一种将其应用于最后一个匹配组的简便方法。如果没有指定组,.start().end()等,似乎总是与第一集团,其中没有关于最后一场比赛中的位置信息。但是,选择组似乎首先需要将返回值临时保存在变量中(这防止了整齐的直线),因为我需要访问有关选择最后一个组的信息,然后.end()从该组中进行选择。

您对此的pythonic解决方案是什么?我认为拥有pythonic比拥有最优化的运行时更有价值。

更新资料

该解决方案在极端情况下也应起作用,例如123(没有与正则表达式匹配的位置),空字符串等。它不应崩溃,例如由于选择了一个空列表的最后一个索引。但是,因为即使是我在问题中上面的丑陋答案也需要多于一行,所以我想单行可能是不可能的(仅仅是因为需要检查re.search()re.finditer()在处理返回值之前)。由于这个原因,我将接受针对此答案的pythonic多行解决方案。

python regex string regex-negation

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

如何在 Python 项目中正确构建内部脚本?

考虑以下 Python 项目框架:

proj/
??? foo
?   ??? __init__.py
??? README.md
??? scripts
    ??? run.py
Run Code Online (Sandbox Code Playgroud)

在这种情况下foo保存主项目文件,例如

# foo/__init__.py
class Foo():
    def run(self):
        print('Running...')
Run Code Online (Sandbox Code Playgroud)

scripts保存需要从 导入文件的辅助脚本,foo然后通过以下方式调用:

[~/proj]$ python scripts/run.py
Run Code Online (Sandbox Code Playgroud)

有两种导入方式Foo都失败了:

  1. 如果尝试进行相对导入,from ..foo import Foo则错误为ValueError: attempted relative import beyond top-level package
  2. 如果尝试绝对导入,from foo import Foo则错误为ModuleNotFoundError: No module named 'foo'

我目前的解决方法是将运行路径附加到sys.path

import sys
sys.path.append('.')

from foo import Foo
Foo().run()
Run Code Online (Sandbox Code Playgroud)

但这感觉就像一个黑客,必须添加到scripts/.

有没有更好的方法来构建此类项目中的脚本?

python pythonpath python-3.x

12
推荐指数
3
解决办法
1548
查看次数

这个错误是什么意思:“TypeError:泛型类型的参数必须是类型”?

我不确定这个错误是什么意思:

TypeError: Parameters to generic types must be types. Got slice(typing.List, <class 'int'>, None).
Run Code Online (Sandbox Code Playgroud)

我试图确认矩阵中是否有给定的单元格/索引。(在矩阵中[[A, B, C], [D, E, F]]是否[0, 2]存在单元格/索引?在 C 处是)。

我的输入参数是一个指定单元格索引的列表。我想获取单元格/列表并修改它以检查它是否存在。每次我尝试触摸参数列表时,都会出现错误。

def in_matrix(matr: List[List:int], cell: List[int]) -> bool:
    b = cell.pop()
    a = cell.pop()
    print(a)
    print(b)
    for y in range(len(matr)):
        for x in range(len(matr[y])):
            if matr[a][b] == True:
                return True
            else:
                return False
Run Code Online (Sandbox Code Playgroud)

python typeerror python-3.x

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

是否可以在Python实例级别覆盖__getitem__?

使用以下代码:

import types

class Foo():
    def __getitem__(self, x):
        return x

def new_get(self, x):
    return x + 1

x = Foo()
x.__getitem__ = types.MethodType(new_get, x)
Run Code Online (Sandbox Code Playgroud)

x.__getitem__(42)将返回43,但x[42]将返回42。

有没有一种方法可以__getitem__在Python实例级别进行覆盖?

python monkeypatching

11
推荐指数
2
解决办法
222
查看次数

我可以撤消 Black 对我的 Python 代码所做的格式样式更改吗?

Python 格式化工具 Black 是否有一个选项可以撤消它在运行后所做的格式化更改?还是假设我正在使用源代码控制并制作自己的备份?这是截至 2019 年 12 月和黑色版本 19.3b0。

python python-black

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

什么是异常传播?

什么是 异常传播

我试过谷歌但不满意结果.如果可能的话,还请举一些例子来解释.C++,php和java语言更可取.

java exception

10
推荐指数
3
解决办法
3万
查看次数

Why define create_foo() in a Django models.Manager instead of overriding create()?

Reading the Django docs, it advices to make a custom creation method for a model named Foo by defining it as create_foo in the manager:

class BookManager(models.Manager):
    def create_book(self, title):
        book = self.create(title=title)
        # do something with the book
        return book

class Book(models.Model):
    title = models.CharField(max_length=100)

    objects = BookManager()

book = Book.objects.create_book("Pride and Prejudice")
Run Code Online (Sandbox Code Playgroud)

My question is that why is the previous one preferred to simply overriding the base class's create method:

class BookManager(models.Manager):
    def create(self, title):
        book = …
Run Code Online (Sandbox Code Playgroud)

python django django-models django-managers

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