字典在Python 3.6中排序(至少在CPython实现下),与之前的版本不同.这似乎是一个重大变化,但它只是文档中的一小段.它被描述为CPython实现细节而不是语言特性,但也暗示这可能成为未来的标准.
在保留元素顺序的同时,新字典实现如何比旧字典实现更好?
以下是文档中的文字:
dict()
现在使用PyPy开创的"紧凑"表示.与Python 3.5相比,新dict()的内存使用量减少了20%到25%.PEP 468(在函数中保留**kwargs的顺序.)由此实现.这个新实现的顺序保留方面被认为是一个实现细节,不应该依赖(这可能会在未来发生变化,但是在更改语言规范之前,希望在几种版本的语言中使用这个新的dict实现为所有当前和未来的Python实现强制命令保留语义;这也有助于保持与随机迭代顺序仍然有效的语言的旧版本的向后兼容性,例如Python 3.5).(由INADA Naoki在issue 27350中提供.最初由Raymond Hettinger提出的想法.)
2017年12月更新:Python 3.7 保证了dict
保留插入顺序
我试图从控制台运行一个模块.我的目录结构是这样的:
我正在尝试运行模块p_03_using_bisection_search.py
,从problem_set_02
目录使用:
$ python3 p_03_using_bisection_search.py
Run Code Online (Sandbox Code Playgroud)
里面的代码p_03_using_bisection_search.py
是:
__author__ = 'm'
from .p_02_paying_debt_off_in_a_year import compute_balance_after
def compute_bounds(balance: float,
annual_interest_rate: float) -> (float, float):
# there is code here, but I have omitted it to save space
pass
def compute_lowest_payment(balance: float,
annual_interest_rate: float) -> float:
# there is code here, but I have omitted it to save space
pass
def main():
balance = eval(input('Enter the initial balance: '))
annual_interest_rate = eval(input('Enter the annual interest rate: '))
lowest_payment …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一个相当简单的Python程序转换为可执行文件,但找不到我想要的内容,所以我有几个问题(我正在运行Python3.6):
到目前为止我发现的这种方法如下
pyinstaller/py2exe
这是我尝试过的/我遇到的问题.
pyinstaller
在它之前安装了所需的下载(pypi-something),所以它没有用.下载必备文件后,pyinstaller
仍然无法识别它.Python 3.6即将发布.PEP 494 - Python 3.6发布时间表提到12月底,所以我通过Python 3.6中的新功能看到他们提到了变量注释:
PEP 484引入了函数参数类型注释的标准,即类型提示.此PEP为Python添加语法以注释变量类型,包括类变量和实例变量:
Run Code Online (Sandbox Code Playgroud)primes: List[int] = [] captain: str # Note: no initial value! class Starship: stats: Dict[str, int] = {}
与函数注释一样,Python解释器不会将任何特定含义附加到变量注释,只将它们存储在
__annotations__
类或模块的特殊属性中.与静态类型语言中的变量声明相比,注释语法的目标是提供一种通过抽象语法树和__annotations__
属性为第三方工具和库指定结构化类型元数据的简便方法.
因此,根据我的阅读,它们是来自Python 3.5的类型提示的一部分,在Python 3.5中的什么是类型提示中有所描述.
我按照captain: str
和class Starship
示例,但不确定最后一个:如何primes: List[int] = []
解释?它是否定义了一个只允许整数的空列表?
我正在使用模板字符串来生成一些文件,我喜欢为此目的的新f字符串的简洁性,以减少我之前的模板代码,如下所示:
template_a = "The current name is {name}"
names = ["foo", "bar"]
for name in names:
print (template_a.format(**locals()))
Run Code Online (Sandbox Code Playgroud)
现在我可以这样做,直接替换变量:
names = ["foo", "bar"]
for name in names:
print (f"The current name is {name}")
Run Code Online (Sandbox Code Playgroud)
但是,有时在其他地方定义模板是有意义的 - 在代码中更高,或从文件或其他东西导入.这意味着模板是一个带有格式标签的静态字符串.必须在字符串上发生一些事情,告诉解释器将字符串解释为新的f字符串,但我不知道是否有这样的事情.
有没有办法引入一个字符串并将其解释为f字符串以避免使用该.format(**locals())
调用?
理想情况下,我希望能够像这样编码......(magic_fstring_function
我不理解的部分在哪里进来):
template_a = f"The current name is {name}"
# OR [Ideal2] template_a = magic_fstring_function(open('template.txt').read())
names = ["foo", "bar"]
for name in names:
print (template_a)
Run Code Online (Sandbox Code Playgroud)
...使用此期望的输出(不读取文件两次):
The current name is foo
The current name is bar
Run Code Online (Sandbox Code Playgroud)
...但我得到的实际输出是:
The current …
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用f-strings以Pythonic方式格式化这种情况:
names = ['Adam', 'Bob', 'Cyril']
text = f"Winners are:\n{'\n'.join(names)}"
print(text)
Run Code Online (Sandbox Code Playgroud)
问题是'\'
不能在f-string中使用.预期产量:
Winners are:
Adam
Bob
Cyril
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python3.7将tensorflow安装到Mac上.但是,我收到错误:
$ pip3 -v install tensorflow
...
Skipping link https://files.pythonhosted.org/packages/56/7a/c6bca0fe52a94ca508731d8b139e7dbd5a36cddc64c19f422f97e5a853e8/tensorflow-1.10.0rc1-cp36-cp36m-win_amd64.whl#sha256=3ab24374888d6a13d55ce2e3cf4ba0c9cd6f824723313db5322512087525cb78 (from https://pypi.org/simple/tensorflow/); it is not compatible with this Python
Could not find a version that satisfies the requirement tensorflow (from versions: )
Cleaning up...
Removed build tracker '/private/var/folders/4n/9342s4wd3jv0qzwjz8rxrygr0000gp/T/pip-req-tracker-3p60r2lo'
No matching distribution found for tensorflow
Run Code Online (Sandbox Code Playgroud)
从我可以收集到的这种情况正在发生,因为tensorflow还不支持Python3.7.作为一种解决方法,我想在3.7和3.7之间安装Python3.6,然后将tensorflow安装到该版本.但是,我是Mac的新手,并且不确定正确的方法来做到这一点,而不会有可能搞乱已有的Python版本.
我已经尝试过使用brew,但它看起来像Python3一样具体.做我正在做的事情的正确方法是什么?
我想在for
-loop中注释一个变量类型.我试过这个:
for i: int in range(5):
pass
Run Code Online (Sandbox Code Playgroud)
但显然它没有用.
我期望在PyCharm 2016.3.2中进行自动完成工作.像这样的预注释:
i: int
for i in range(5):
pass
Run Code Online (Sandbox Code Playgroud)
没有帮助.
PS预注释适用于PyCharm> = 2017.1
我正在尝试使用Python 3.6.通过新代码,我偶然发现了这个新语法:
f"My formatting string!"
Run Code Online (Sandbox Code Playgroud)
看来我们可以这样做:
>>> name = "George"
>>> print(f"My cool string is called {name}.")
My cool string is called George.
Run Code Online (Sandbox Code Playgroud)
任何人都可以对这个内部运作有所了解吗?特别是f前缀字符串可以采用的变量范围是多少?
我正在尝试在基于arm的Linux机器上编译Python 3.6,
./configure
输出:
如果您想要一个激活所有优化的版本构建(LTO,PGO等),请运行
./configure --enable-optimizations
.
怎么--enable-optimizations
办?
python-3.6 ×10
python ×9
python-3.x ×6
f-string ×3
type-hinting ×2
annotations ×1
configure ×1
dictionary ×1
exe ×1
for-loop ×1
linux ×1
macos ×1
module ×1
newline ×1
py2exe ×1
pycharm ×1
python-3.7 ×1
scope ×1
tensorflow ×1