在Python 2.7中,我可以将字典键,值或项目作为列表获取:
>>> newdict = {1:0, 2:0, 3:0}
>>> newdict.keys()
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
现在,在Python> = 3.3中,我得到这样的东西:
>>> newdict.keys()
dict_keys([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
所以,我必须这样做以获得一个列表:
newlist = list()
for i in newdict.keys():
newlist.append(i)
Run Code Online (Sandbox Code Playgroud)
我想知道,有没有更好的方法来返回Python 3中的列表?
在查看Python 3.3语法规范时,我最近发现了一些有趣的东西:
funcdef: 'def' NAME parameters ['->' test] ':' suite
Run Code Online (Sandbox Code Playgroud)
Python 2中没有可选的"箭头"块,我在Python 3中找不到任何有关其含义的信息.事实证明这是正确的Python并且它被解释器接受:
def f(x) -> 123:
return x
Run Code Online (Sandbox Code Playgroud)
我认为这可能是某种先决条件语法,但是:
x在这里测试,它仍未定义,2 < 1),它都不会影响功能行为.任何习惯这种语法的人都可以解释一下吗?
如何用Python文字将整数表示为二进制数?
我很容易找到十六进制的答案:
>>> 0x12AF
4783
>>> 0x100
256
Run Code Online (Sandbox Code Playgroud)
和八进制:
>>> 01267
695
>>> 0100
64
Run Code Online (Sandbox Code Playgroud)
你如何使用文字在Python中表达二进制文件?
答案摘要
int('01010101111',2)文字来表示二进制文件,但不能用文字表达.0b1100111或0B1100111.0o27或0O27(第二个字符是字母O)表示八进制.027octals 的旧语法.我正在尝试制作一个包含数字的列表1-1000.显然,写入/读取会很烦人,所以我试图制作一个带有范围的列表.在Python 2中,似乎:
some_list = range(1,1000)
Run Code Online (Sandbox Code Playgroud)
会有用,但在Python 3中,范围类似于xrangePython 2?
任何人都可以对此提供一些见解吗?
假设我有一个功能:
def get_some_date(some_argument: int=None) -> %datetime_or_None%:
if some_argument is not None and some_argument == 1:
return datetime.utcnow()
else:
return None
Run Code Online (Sandbox Code Playgroud)
如何为可能的内容指定返回类型None?
以下两个代码片段之间是否存在任何最终差异?第一个为函数中的变量赋值,然后返回该变量.第二个函数只是直接返回值.
Python会将它们转换为等效的字节码吗?其中一个更快吗?
案例1:
def func():
a = 42
return a
Run Code Online (Sandbox Code Playgroud)
案例2:
def func():
return 42
Run Code Online (Sandbox Code Playgroud) 一个tuple发生在Python的内存空间少:
>>> a = (1,2,3)
>>> a.__sizeof__()
48
Run Code Online (Sandbox Code Playgroud)
而lists需要更多的内存空间:
>>> b = [1,2,3]
>>> b.__sizeof__()
64
Run Code Online (Sandbox Code Playgroud)
Python内存管理内部会发生什么?
我想"FOO"在Python 环境中检查我的环境是否存在变量.为此,我使用的是os标准库.在阅读了图书馆的文档后,我找到了两种方法来实现我的目标:
方法1:
if "FOO" in os.environ:
pass
Run Code Online (Sandbox Code Playgroud)
方法2:
if os.getenv("FOO") is not None:
pass
Run Code Online (Sandbox Code Playgroud)
我想知道哪种方法(如果有的话)是好的/首选条件以及为什么.
python if-statement environment-variables python-2.7 python-3.x
我想在我当前的Python 3.5项目中使用类型提示.我的函数应该接收一个函数作为参数.
如何在类型提示中指定类型函数?
import typing
def my_function(name:typing.AnyStr, func: typing.Function) -> None:
# However, typing.Function does not exist.
# How can I specify the type function for the parameter `func`?
# do some processing
pass
Run Code Online (Sandbox Code Playgroud)
我检查了PEP 483,但在那里找不到函数类型提示.
import sys
x = 'ñ'
print(sys.getsizeof(x))
int(x) #throws an error
print(sys.getsizeof(x))
Run Code Online (Sandbox Code Playgroud)
我们得到74,然后是两个getsizeof调用的77个字节.
看起来我们正在从失败的int调用中向对象添加3个字节.
来自twitter的更多示例(您可能需要重新启动python以将大小重置为74):
x = 'ñ'
y = 'ñ'
int(x)
print(sys.getsizeof(y))
Run Code Online (Sandbox Code Playgroud)
77!
print(sys.getsizeof('ñ'))
int('ñ')
print(sys.getsizeof('ñ'))
Run Code Online (Sandbox Code Playgroud)
74,然后77.
python ×10
python-3.x ×7
list ×3
python-2.7 ×2
python-3.5 ×2
type-hinting ×2
annotations ×1
binary ×1
dictionary ×1
if-statement ×1
integer ×1
interpreter ×1
literals ×1
mypy ×1
python-2.x ×1
range ×1
string ×1
syntax ×1
tuples ×1
unicode ×1