小编Kar*_*tel的帖子

为什么双重否定会改变 C++ 概念的价值?

我的一个朋友向我展示了一个带有概念的 C++20 程序,这让我感到困惑:

struct A { static constexpr bool a = true; };

template <typename T>
concept C = T::a || T::b;

template <typename T>
concept D = !!(T::a || T::b);

static_assert( C<A> );
static_assert( !D<A> );
Run Code Online (Sandbox Code Playgroud)

它被所有编译器接受:https : //gcc.godbolt.org/z/e67qKoqce

这里的概念与概念D相同C,唯一的区别是双重否定运算符!!,乍一看不会改变概念值。仍然对于 struct 来说,A这个概念C是正确的,而这个概念D是错误的。

你能解释一下为什么会这样吗?

c++ language-lawyer c++-concepts c++20

49
推荐指数
2
解决办法
2231
查看次数

"Flat比嵌套好" - 对于数据和代码?

这个问题让我思考:我们应该将"扁平比嵌套更好"的原则应用于数据还是代码?即使数据存在"逻辑树结构"?

在这种情况下,我认为这意味着将子代表作为ID列表,而不是实际的子列表,其中所有节点都在一个列表中:

[ {'id': 4, 'children': ()},
  {'id': 2, 'children': (1, 7)},
  {'id': 1, 'children': (6, 5)},
  {'id': 6, 'children': ()},
  {'id': 5, 'children': ()},
  {'id': 7, 'children': (3,)},
  {'id': 3, 'children': ()} ]
Run Code Online (Sandbox Code Playgroud)

(我使用了元组,因为我不愿意给自己灵活地改变一个对象,直到这种灵活性证明它本身是有用的并且可以以清晰的方式使用.无论如何我永远不会None在这里使用而不是空序列,因为它使得复杂化逻辑,"特殊情况不够特别" - 在这里,它并不特别.)

当然这个更短,但树形结构模糊不清.这是否与"明确胜过隐性"相矛盾?

就个人而言,我发现"扁平比嵌套更好"的适用性有限,并且远不及禅宗最重要的方面.(当然,如果我不允许自己进行重要的嵌套,我无法做很多很好的函数式编程工作.)我怀疑"嵌套"的问题是当你理解信息时它需要上下文切换.我真的认为在遵循命令式逻辑时比解析数据或函数式代码更容易出问题 - 在这里更容易精神上命名嵌套块,并将其工作与外部上下文分开考虑.

怎么说你?

python theory

34
推荐指数
4
解决办法
8867
查看次数

为什么Python 2.x中的math.factorial比3.x慢得多?

我在我的机器上得到以下结果:

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('factorial(10000)', 'from math import factorial', number=100)
1.9785256226699202
>>>

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('factorial(10000)', 'from math import factorial', number=100)
9.403801111593792
>>>
Run Code Online (Sandbox Code Playgroud)

我认为这可能与int/long转换有关,但factorial(10000L)在2.7中没有任何更快.

python performance python-2.x factorial python-3.x

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

ModuleNotFoundError:使用 Metaflow 时没有名为“pandas.core.indexes.numeric”的模块

我使用 Metaflow 加载数据框。它已成功从工件存储中取消腌制,但当我尝试使用 查看其索引时df.index,收到一条错误消息ModuleNotFoundError: No module named 'pandas.core.indexes.numeric': 。为什么?

我在这里这里查看了具有类似错误消息的其他答案,这些答案说这是由于尝试使用旧版本的 Pandas 来取消数据帧引起的。然而,我的错误略有不同,并且不能通过升级 Pandas ( pip install pandas -U) 来修复。

python pickle pandas netflix-metaflow

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

Iron Python:Iron Python有什么用处

我对python很感兴趣,但是我在.NET环境中工作,所以我正在研究Iron Python,并想知道它将用于什么.

你能写一个应用程序吗?或者是否为您的应用添加脚本语言?

你们是如何使用它的?

.net python ironpython dynamic-language-runtime

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

如何升级pip?

我想安装tensorflow,但我需要升级pip。如何升级pip?我尝试通过命令行升级,这就是我得到的。

C:\Users\garoo>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'c:\\program files (x86)\\python36-32\\lib\\site-packages\\pip-10.0.1.dist-info\\entry_points.txt'
Consider using the `--user` option or check the permissions.

You are using pip version 10.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\garoo>python -m pip install --upgrade pip
Run Code Online (Sandbox Code Playgroud)

python windows pip

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

无法使用 Python 3.11 安装 PyTorch (Windows)

我最近升级到 Python 3.11 并继续安装我通常用于 3.11 的库。我用 一项一项地浏览了我的清单pip

当我尝试安装 PyTorch 时,出现错误:

ERROR: Could not find a version that satisfies the requirement torch (from versions: none)
ERROR: No matching distribution found for torch
Run Code Online (Sandbox Code Playgroud)

我在 pytorch.com 上找到的建议都没有解决该问题。

出了什么问题,我需要做什么才能正确安装 PyTorch?

python pip pytorch python-3.11

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

为什么 tkinter(或海龟)似乎丢失或损坏?它不应该是标准库的一部分吗?

当尝试使用 Tkinter 标准库包或其相关功能(海龟图形使用turtle和内置 IDLE IDE)或使用将此作为依赖项的第三方库(例如显示使用 Matplotlib 的图形窗口)。

似乎即使不存在因隐藏标准库模块名称而引起的问题(对于尝试遵循教程并使用海龟图形的初学者来说,这是一个常见问题 -示例 1示例 2示例 3示例 4),通常会发生标准库 Tkinter 无法工作的情况。这是一个大问题,因为许多初学者再次尝试遵循使用海龟图形的教程,并盲目地假设turtle标准库将会存在。

可能会报错:

当 Tkinter 被记录为标准库的一部分时,为什么会出现这样的问题?如何添加或修复缺少的标准库功能?对于特定的 …

python installation tkinter modulenotfounderror

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

如何使用一组通用键正确散列字典以实现重复数据删除目的?

我有一些日志数据,例如:

logs = [
 {'id': '1234', 'error': None, 'fruit': 'orange'},
 {'id': '12345', 'error': None, 'fruit': 'apple'}
]
Run Code Online (Sandbox Code Playgroud)

每个字典都有相同的键:'id''error''fruit'(在本例中)。

我想从此列表中删除重复项,但直接dictset基于基础的方法不起作用,因为我的元素本身就是dicts,不可散列

>>> set(logs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
Run Code Online (Sandbox Code Playgroud)

另一种方法是排序并使用 itertools.groupby - 但字典也不具有可比性,因此这也不起作用:

>>> from itertools import groupby
>>> [k for k, _ in groupby(sorted(logs))]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module> …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

18
推荐指数
2
解决办法
1469
查看次数

PyCharm 无法识别 Python 3.10,如何配置?

我正在尝试在 Windows 10 上将 Python 3.10 与 PyCharm 结合使用。

然而,它被识别为 Python 3.1:

在此输入图像描述

如果我尝试运行我的脚本,解释器会抱怨这个版本的 Python 已被弃用。

我该如何解决这个问题?

python pycharm

15
推荐指数
2
解决办法
2万
查看次数