我正在研究Python 2.7中的集合教程,我遇到了一个使用我不理解的for
循环的行为,我试图找出导致差异的原因是什么。输出可能是。
练习的目的是使用for循环从包含由城市对冻结集组成的键的字典中生成一组城市。
数据来自以下字典:
flight_distances = {
frozenset(['Atlanta', 'Chicago']): 590.0,
frozenset(['Atlanta', 'Dallas']): 720.0,
frozenset(['Atlanta', 'Houston']): 700.0,
frozenset(['Atlanta', 'New York']): 750.0,
frozenset(['Austin', 'Dallas']): 180.0,
frozenset(['Austin', 'Houston']): 150.0,
frozenset(['Boston', 'Chicago']): 850.0,
frozenset(['Boston', 'Miami']): 1260.0,
frozenset(['Boston', 'New York']): 190.0,
frozenset(['Chicago', 'Denver']): 920.0,
frozenset(['Chicago', 'Houston']): 940.0,
frozenset(['Chicago', 'Los Angeles']): 1740.0,
frozenset(['Chicago', 'New York']): 710.0,
frozenset(['Chicago', 'Seattle']): 1730.0,
frozenset(['Dallas', 'Denver']): 660.0,
frozenset(['Dallas', 'Los Angeles']): 1240.0,
frozenset(['Dallas', 'New York']): 1370.0,
frozenset(['Denver', 'Los Angeles']): 830.0,
frozenset(['Denver', 'New York']): 1630.0,
frozenset(['Denver', 'Seattle']): 1020.0,
frozenset(['Houston', 'Los Angeles']): 1370.0,
frozenset(['Houston', 'Miami']): 970.0,
frozenset(['Houston', 'San Francisco']): 1640.0,
frozenset(['Los Angeles', 'New York']): 2450.0,
frozenset(['Los Angeles', 'San Francisco']): 350.0,
frozenset(['Los Angeles', 'Seattle']): 960.0,
frozenset(['Miami', 'New York']): 1090.0,
frozenset(['New York', 'San Francisco']): 2570.0,
frozenset(['San Francisco', 'Seattle']): 680.0,
}
还有一个测试列表可以创建预期的集合作为检查:
flying_circus_cities = [
'Houston', 'Chicago', 'Miami', 'Boston', 'Dallas', 'Denver',
'New York', 'Los Angeles', 'San Francisco', 'Atlanta',
'Seattle', 'Austin'
]
当代码以下面的形式编写时,循环产生预期的结果。
cities = set()
for pair in flight_distances:
cities = cities.union(pair)
print cities
print "Check:", cities == set(flying_circus_cities)
输出:
set(['Houston', 'Chicago', 'Miami', 'Boston', 'Dallas', 'Denver', 'New York', 'Los Angeles', 'San Francisco', 'Atlanta', 'Seattle', 'Austin'])
Check: True
但是,如果我尝试理解,使用以下任何一种方法,我会得到不同的结果。
cities = set()
cities = {pair for pair in flight_distances}
print cities
print "Check:", cites == set(flying_circus_cities)
或
cities = set()
cities = cities.union(pair for pair in flight_distances)
print cities
print "Check:", cities == set(flying_circus_cities)
两者的输出:
set([frozenset(['Atlanta', 'Dallas']), frozenset(['San Francisco', 'New York']), frozenset(['Denver', 'Chicago']), frozenset(['Houston', 'San Francisco']), frozenset(['San Francisco', 'Austin']), frozenset(['Seattle', 'Los Angeles']), frozenset(['Boston', 'New York']), frozenset(['Houston', 'Atlanta']), frozenset(['New York', 'Chicago']), frozenset(['San Francisco', 'Seattle']), frozenset(['Austin', 'Dallas']), frozenset(['New York', 'Dallas']), frozenset(['Houston', 'Chicago']), frozenset(['Seattle', 'Denver']), frozenset(['Seattle', 'Chicago']), frozenset(['Miami', 'New York']), frozenset(['Los Angeles', 'Denver']), frozenset(['Miami', 'Houston']), frozenset(['San Francisco', 'Los Angeles']), frozenset(['New York', 'Denver']), frozenset(['Atlanta', 'Chicago']), frozenset(['Boston', 'Chicago']), frozenset(['Houston', 'Austin']), frozenset(['Houston', 'Los Angeles']), frozenset(['New York', 'Los Angeles']), frozenset(['Atlanta', 'New York']), frozenset(['Denver', 'Dallas']), frozenset(['Los Angeles', 'Dallas']), frozenset(['Los Angeles', 'Chicago'])])
Check: False
我无法弄清楚为什么第一个示例中的for循环按预期解包对,以便它生成一个包含每个城市的一个实例的集合,同时尝试编写循环作为理解拉出frozenset([city1, city2])
配对并将它们放在集合中。
我不明白为什么pair
会在第一个实例中给出城市字符串,但在第二个实例中传递冻结集。
有人可以解释不同的行为吗?
注意:正如Holt和donkopotamus所解释的那样,为什么这种行为方式不同的问题是,在进行单一作业之前,使用理解完全评估了整个字典到cities
变量,从而创建一组frozensets,其中标准for
循环一次一个地解包对,并分别评估每个对,将它们分配给cities
一个每次传递for
循环的时间,并允许union函数评估传递给它的对的每个实例。
他们进一步解释说,使用*
-operator在理解中解压缩字典以产生所需的行为。
cities = cities.union(*(set(pair) for pair in flight_distances))
答案 0 :(得分:1)
表达式:
cities = set()
cities = cities.union(pair for pair in flight_distances)
将空集{}
与另一集
{pair_0, pair_1, pair_2, ..., pair_n}
给你一套套装。
相比之下,以下内容将为您提供所有飞往的城市:
>>> set.union(*(set(pair) for pair in flight_distances))
{'Atlanta',
'Austin',
'Boston',
'Chicago',
'Dallas',
'Denver',
'Houston',
'Los Angeles',
'Miami',
'New York',
'San Francisco',
'Seattle'}
这里我们将每个冻结的集合密钥转换为普通集合并找到联合。
答案 1 :(得分:0)
在第一个版本中,pair
在每个循环中都是frozenset
,因此您可以使用它union
,而在您的版本中,您尝试使用{ {1}}的{1}}。
第一种情况归结为(在每次迭代时与set
联合):
frozenset
所以你有(数学上):
frozenset
在您的(最后一个)案例中,您正在执行以下操作(一个序列为cities = set()
cities.union(frozenset(['Atlanta', 'Chicago']))
cities.union(frozenset(['Atlanta', 'Dallas']))
...
的联合):
cities = {} # Empty set
cities = {} U {'Atlanta', 'Chicago'} = {'Atlanta', 'Chicago'}
cities = {'Atlanta', 'Chicago'} U {'Atlanta', 'Dallas'} = {'Atlanta', 'Chicago', 'Dallas'}
...
所以你有:
frozenset
由于没有两对是相同的,因此您将获得初始字典中所有对的一组,因为您传递的是cities = set()
cities.union([frozenset(['Atlanta', 'Chicago']), frozenset(['Atlanta', 'Dallas']), ...])
cities = {}
cities = {} U {{'Atlanta', 'Chicago'}, {'Atlanta', 'Dallas'}, ...}
= {{'Atlanta', 'Chicago'}, {'Atlanta', 'Dallas'}, ...} # Nothing disappears
(对)城市,而不是{{1}城市到set
。
从更抽象的角度来看,你正试图获得:
set
使用:
set