yieldPython中关键字的用途是什么?它有什么作用?
例如,我试图理解这段代码1:
def _get_child_candidates(self, distance, min_dist, max_dist):
if self._leftchild and distance - max_dist < self._median:
yield self._leftchild
if self._rightchild and distance + max_dist >= self._median:
yield self._rightchild
Run Code Online (Sandbox Code Playgroud)
这是来电者:
result, candidates = [], [self]
while candidates:
node = candidates.pop()
distance = node._get_dist(obj)
if distance <= max_dist and distance >= min_dist:
result.extend(node._values)
candidates.extend(node._get_child_candidates(distance, min_dist, max_dist))
return result
Run Code Online (Sandbox Code Playgroud)
_get_child_candidates调用该方法时会发生什么?列表是否返回?单个元素?它又被召唤了吗?后续通话何时停止?
1.代码来自Jochen Schulz(jrschulz),他为度量空间创建了一个很棒的Python库.这是完整源代码的链接:模块mspace.
在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中的列表?
在闲置浏览命名空间的同时,我注意到一个名为" Ellipsis " 的奇怪物体,它似乎没有或做任何特殊的事情,但它是一个全局可用的内置.
在搜索之后,我发现它被Numpy和Scipy用于切片语法的一些模糊变体......但几乎没有别的.
这个对象是否专门用于支持Numpy + Scipy?省略号是否具有任何通用含义或用途?
D:\workspace\numpy>python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> Ellipsis
Ellipsis
Run Code Online (Sandbox Code Playgroud) 我需要列表的最后9个数字,我确信有一种方法可以用切片来做,但我似乎无法得到它.我可以像这样获得前9个:
num_list[0:9]
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.
你能想出一个很好的方法(可能用itertools)将迭代器拆分成给定大小的块吗?
因此l=[1,2,3,4,5,6,7]与chunks(l,3)变成一个迭代[1,2,3], [4,5,6], [7]
我可以想到一个小程序来做这个,但不是一个很好的方式可能itertools.
我在Nutshell中研究Alex Marteli的Python,书中暗示任何具有next()方法的对象(或者至少可以用作迭代器).它还表明大多数迭代器是通过对被调用方法的隐式或显式调用构建的iter.
在书中读到这篇文章后,我感受到了尝试它的冲动.我启动了一个python 2.7.3解释器,并做到了这一点:
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for number in range(0, 10):
... print x.next()
Run Code Online (Sandbox Code Playgroud)
但结果如下:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AttributeError: 'list' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)
在混乱中,我试图研究x对象的结构,dir(x)我注意到它有一个__iter__函数对象.所以我发现它可以用作迭代器,只要它支持那种类型的接口.
所以当我再次尝试时,这一次略有不同,尝试这样做:
>>> _temp_iter = next(x)
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list object …Run Code Online (Sandbox Code Playgroud) 此代码来自Python的文档.我有点困惑.
words = ['cat', 'window', 'defenestrate']
for w in words[:]:
if len(w) > 6:
words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)
以下是我最初的想法:
words = ['cat', 'window', 'defenestrate']
for w in words:
if len(w) > 6:
words.insert(0, w)
print(words)
Run Code Online (Sandbox Code Playgroud)
为什么这段代码创建了一个无限循环而第一个没有?
是什么让Python中的东西可迭代?即.可以用它循环for
我可以在Python中创建一个可迭代的类吗?如果是这样,怎么样?
python ×10
iterator ×4
list ×4
coroutine ×1
dictionary ×1
ellipsis ×1
for-loop ×1
generator ×1
iteration ×1
object ×1
python-2.x ×1
python-3.x ×1
set ×1
slice ×1
yield ×1