【发布时间】:2016-05-18 09:53:17
【问题描述】:
我有一个集合,其中包含 n 组素数(3):
>>> sets
{frozenset({3, 13, 23}), frozenset({17, 2, 13}),
frozenset({19, 2, 3}), frozenset({3, 29, 23}), frozenset({17, 11, 23}),
frozenset({17, 2, 19}), frozenset({11, 17, 3}), frozenset({17, 5, 7})}
我想创建具有值的字典:素数三元组和键:三个素数的乘积。
这是我的尝试:
lists = [list(i) for i in sets]
products = [reduce(lambda x,y:x*y,i) for i in lists]
dictir = {x:y for x in products for y in sets}
但是 dictir 给了我不正确的结果:
{897: frozenset({17, 5, 7}), 114: frozenset({17, 5, 7}), 595: frozenset({17, 5, 7}), 561: frozenset({17, 5, 7}), 646: frozenset({17, 5, 7}), 2001: frozenset({17, 5, 7}), 442: frozenset({17, 5, 7}), 4301: frozenset({17, 5, 7})}
你能帮我纠正一下吗?
【问题讨论】:
标签: python dictionary set dictionary-comprehension