0

我正在学习 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会在第一个实例中给出城市字符串,但在第二个实例中传递 freezeset。

有人可以解释不同的行为吗?

注意:正如Holtdonkopotamus所解释的,为什么这种行为不同的问题是,在对变量进行单个赋值之前,使用理解完全评估了整个字典cities,从而创建了一组冻结集,其中标准for循环解包一次一对一对并分别评估每个单独的一对cities,每次循环将它们分配给一个,for并允许联合函数评估传递给它的对的每个实例。

他们进一步解释说,使用*-operator在理解中解包字典以产生所需的行为。

cities = cities.union(*(set(pair) for pair in flight_distances))
4

2 回答 2

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'}

在这里,我们将每个冻结的集合键转换为一个普通集合并找到并集。

于 2016-06-27T08:55:17.677 回答
0

在第一个版本中,pairis a frozensetat each loop,所以你可以用它做 a ,而在你的版本中,你尝试用 a做union一个 union setfrozenset

第一种情况归结为(frozenset在每次迭代中与 a 联合):

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

由于没有两对是相同的,因此您会在初始字典中获得一组所有对,因为您将 a setof set(pair) of city 传递给 ,而不是 a setof city to .union()


从更抽象的角度来看,您试图获得:

S = {} U S1 U S2 U S3 U ... U Sn = (((({} U S1) U S2) U S3) U ...) U Sn

和:

S = {} U {S1, S2, S3, ..., Sn}
于 2016-06-27T08:58:56.687 回答