- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
出于不相关的原因,我以某种方式组合了一些数据结构,同时还用 OrderedDict
替换了 Python 2.7 的默认 dict
。数据结构使用元组作为字典中的键。请忽略这些细节(dict
类型的替换在下面没有用,但它在真实代码中)。
import __builtin__
import collections
import contextlib
import itertools
def combine(config_a, config_b):
return (dict(first, **second) for first, second in itertools.product(config_a, config_b))
@contextlib.contextmanager
def dict_as_ordereddict():
dict_orig = __builtin__.dict
try:
__builtin__.dict = collections.OrderedDict
yield
finally:
__builtin__.dict = dict_orig
这最初按预期工作(dict
可以将非字符串关键字参数作为一种特殊情况):
print 'one level nesting'
with dict_as_ordereddict():
result = combine(
[{(0, 1): 'a', (2, 3): 'b'}],
[{(4, 5): 'c', (6, 7): 'd'}]
)
print list(result)
print
输出:
one level nesting
[{(0, 1): 'a', (4, 5): 'c', (2, 3): 'b', (6, 7): 'd'}]
但是,当嵌套调用combine
生成器表达式时,可以看到dict
引用被视为OrderedDict
,缺少dict
使用元组作为关键字参数的特殊行为:
print 'two level nesting'
with dict_as_ordereddict():
result = combine(combine(
[{(0, 1): 'a', (2, 3): 'b'}],
[{(4, 5): 'c', (6, 7): 'd'}]
),
[{(8, 9): 'e', (10, 11): 'f'}]
)
print list(result)
print
输出:
two level nesting
Traceback (most recent call last):
File "test.py", line 36, in <module>
[{(8, 9): 'e', (10, 11): 'f'}]
File "test.py", line 8, in combine
return (dict(first, **second) for first, second in itertools.product(config_a, config_b))
File "test.py", line 8, in <genexpr>
return (dict(first, **second) for first, second in itertools.product(config_a, config_b))
TypeError: __init__() keywords must be strings
此外,通过 yield
而不是生成器表达式来实现可以解决问题:
def combine_yield(config_a, config_b):
for first, second in itertools.product(config_a, config_b):
yield dict(first, **second)
print 'two level nesting, yield'
with dict_as_ordereddict():
result = combine_yield(combine_yield(
[{(0, 1): 'a', (2, 3): 'b'}],
[{(4, 5): 'c', (6, 7): 'd'}]
),
[{(8, 9): 'e', (10, 11): 'f'}]
)
print list(result)
print
输出:
two level nesting, yield
[{(0, 1): 'a', (8, 9): 'e', (2, 3): 'b', (4, 5): 'c', (6, 7): 'd', (10, 11): 'f'}]
问题:
yield
的版本有效?最佳答案
在进入细节之前请注意以下几点: itertools.product
评估迭代器参数以计算产品。这可以从文档中等效的 Python 实现中看出(第一行是相关的):
def product(*args, **kwds):
pools = map(tuple, args) * kwds.get('repeat', 1)
...
您也可以使用自定义类和简短的测试脚本来尝试:
import itertools
class Test:
def __init__(self):
self.x = 0
def __iter__(self):
return self
def next(self):
print('next item requested')
if self.x < 5:
self.x += 1
return self.x
raise StopIteration()
t = Test()
itertools.product(t, t)
创建 itertools.product
对象将在输出中显示立即请求所有迭代器项。
这意味着,只要您调用 itertools.product
计算迭代器参数。这很重要,因为在第一种情况下,参数只是两个列表,所以没有问题。然后你评估最后的result
通过list(result
在上下文管理器之后 dict_as_ordereddict
已经返回,所以所有调用 dict
将被解析为正常的内置 dict
.
现在第二个例子内部调用combine
工作仍然正常,现在返回一个生成器表达式,然后将其用作第二个 combine
的参数之一调用 itertools.product
.正如我们在上面看到的,这些参数会立即被求值,因此生成器对象被要求生成它的值。为此,它需要解决 dict
.但是现在我们仍然在上下文管理器中 dict_as_ordereddict
出于这个原因 dict
将被解析为 OrderedDict
它不接受关键字参数的非字符串键。
重要的是要注意这里使用 return
的第一个版本需要创建生成器对象才能返回它。这涉及创建 itertools.product
目的。这意味着这个版本和 itertools.product
一样懒惰.
现在回答为什么 yield
的问题版本有效。通过使用 yield
,调用该函数将返回一个生成器。现在这是一个真正的惰性版本,因为函数体的执行直到请求项目才开始。这意味着内部和外部都不会调用 convert
将开始执行函数体并调用 itertools.product
直到通过 list(result)
请求元素.您可以通过在该函数内和上下文管理器后面放置一个额外的打印语句来检查:
def combine(config_a, config_b):
print 'start'
# return (dict(first, **second) for first, second in itertools.product(config_a, config_b))
for first, second in itertools.product(config_a, config_b):
yield dict(first, **second)
with dict_as_ordereddict():
result = combine(combine(
[{(0, 1): 'a', (2, 3): 'b'}],
[{(4, 5): 'c', (6, 7): 'd'}]
),
[{(8, 9): 'e', (10, 11): 'f'}]
)
print 'end of context manager'
print list(result)
print
随着 yield
version 我们会注意到它打印了以下内容:
end of context manager
start
start
即生成器仅在通过 list(result)
请求结果时启动.这不同于 return
版本(取消上面代码中的注释)。现在你会看到
start
start
并且在到达上下文管理器的末尾之前,错误已经出现。
附带说明一下,为了让您的代码正常工作,替换 dict
需要无效(并且它是第一个版本),所以我根本不明白你为什么要使用那个上下文管理器。其次,dict
文字在 Python 2 中没有排序,关键字参数也没有排序,因此也违背了使用 OrderedDict
的目的。 .另请注意,在 Python 3 中,dict
的非字符串关键字参数行为已被删除,更新任何键的字典的干净方法是使用 dict.update
.
关于python - 生成器表达式与生成器函数以及令人惊讶的急切求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58454141/
我有一个案例,在大多数情况下,对象之间的关系是这样的,因此在关系上预先配置一个渴望(加入)的负载是有意义的。但是现在我遇到了一种情况,我真的不想完成急切的加载。 我是否应该从关系中删除连接负载并将所有
在我的 Grails 项目中,我有以下类: class A { static hasMany = [cs:C] } class B { static hasMany = [cs:C]
想象一下以下简化的 DI 模型: @ApplicationScoped public class A { private B b; @Inject public A(B b)
我使用 MapLoader 将数据从数据存储初始加载到 Hazelcast (InitialLoadMode = EAGER)。我需要从一个物化 View 中加载这些数据,该 View 是为了在加载过
我使用 Hibernate Envers 4.3.10.Final。我有以下两个 JPA 类: public class Factory { private int factoryID;
EJB 似乎被延迟加载 - 每当访问时。 但是,我想急切地初始化它们 - 即每当容器启动时。这是如何实现的(尤其是在 JBoss 中) This topic给出了一些提示,但不是很令人满意。 最佳答案
我是一名优秀的程序员,十分优秀!