我的一个朋友向我展示了一个带有概念的 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
是错误的。
你能解释一下为什么会这样吗?
这个问题让我思考:我们应该将"扁平比嵌套更好"的原则应用于数据还是代码?即使数据存在"逻辑树结构"?
在这种情况下,我认为这意味着将子代表作为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 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很感兴趣,但是我在.NET环境中工作,所以我正在研究Iron Python,并想知道它将用于什么.
你能写一个应用程序吗?或者是否为您的应用添加脚本语言?
你们是如何使用它的?
我想安装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 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?
当尝试使用 Tkinter 标准库包或其相关功能(海龟图形使用turtle
和内置 IDLE IDE)或使用将此作为依赖项的第三方库(例如显示使用 Matplotlib 的图形窗口)。
似乎即使不存在因隐藏标准库模块名称而引起的问题(对于尝试遵循教程并使用海龟图形的初学者来说,这是一个常见问题 -示例 1;示例 2;示例 3;示例 4),通常会发生标准库 Tkinter 无法工作的情况。这是一个大问题,因为许多初学者再次尝试遵循使用海龟图形的教程,并盲目地假设turtle
标准库将会存在。
可能会报错:
作为ModuleNotFoundError: No module named 'tkinter'
; 或ImportError
具有相同消息的 ;或使用不同的大小写(我知道名称从Tkinter
2.x 更改为tkinter
3.x;这是一个不同的问题)。
类似地,但引用内部_tkinter
模块,并显示带有注释的代码“如果失败,您的 Python 可能无法配置 Tk”;或者使用自定义错误消息“请安装 python-tk 包”或类似内容。
当尝试turtle
专门使用时,出现“没有名为turtle的模块” ,或者上述错误之一。
当尝试使用 Matplotlib 显示绘图时;通常,这种情况会在尝试更改后端后发生,默认设置是为了避免尝试使用 Tkinter。
当 Tkinter 被记录为标准库的一部分时,为什么会出现这样的问题?如何添加或修复缺少的标准库功能?对于特定的 …
我有一些日志数据,例如:
logs = [
{'id': '1234', 'error': None, 'fruit': 'orange'},
{'id': '12345', 'error': None, 'fruit': 'apple'}
]
Run Code Online (Sandbox Code Playgroud)
每个字典都有相同的键:'id'
、'error'
和'fruit'
(在本例中)。
我想从此列表中删除重复项,但直接dict
且set
基于基础的方法不起作用,因为我的元素本身就是dict
s,不可散列:
>>> 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) 我正在尝试在 Windows 10 上将 Python 3.10 与 PyCharm 结合使用。
然而,它被识别为 Python 3.1:
如果我尝试运行我的脚本,解释器会抱怨这个版本的 Python 已被弃用。
我该如何解决这个问题?
python ×9
pip ×2
python-3.x ×2
.net ×1
c++ ×1
c++-concepts ×1
c++20 ×1
factorial ×1
installation ×1
ironpython ×1
pandas ×1
performance ×1
pickle ×1
pycharm ×1
python-2.x ×1
python-3.11 ×1
pytorch ×1
theory ×1
tkinter ×1
windows ×1