0

对于人口

popfrance = 10000
popchina = 202434
popcanberra = 102042
popengland = 224309
popgermany = 203454
popgoldcoast = 90323
popmelbourne = 100000
popparis = 224224
popperth = 10000
popsydney = 292403

然后 :

dic = {'canberra': 79693.11338661514,
       'china': 40246.748450913066,
       'england': 3043.004178666758,
       'france': 0.0,
       'germany': 21558.2996208357,
       'gold coast': 67781.1426515405,
       'melbourne': 92804.01912347642,
       'paris': 40908.82213277263,
       'perth': 65046.35819797423,
       'sydney': 43786.0579097594}
distance = 10000
for k,v in dic.iteritems():
    # k points to the key, and v points to the value
    if v < distance:
        print k,"is within distance", distance
    else:    
        print k,"is outside distance",distance

这会产生地名以及它们是否在一定距离内。我早些时候在代码中存储了每个人口的数据,即法国 = 100000 人(不现实,但正在测试)

请问我想知道什么。有没有一种方法可以获取出现在大于列表中的位置的所有数据。即如果距离 = 10 并且法国在距离之外。然后我可以调用法国的人口数据吗?基本上我想要它,以便如果一个地方在距离之外加载它的人口数据。这在python中可以实现吗?

4

1 回答 1

1

你可能想要这个:

distance_dict = {
   'canberra': 79693.11338661514,
   'china': 40246.748450913066,
   'england': 3043.004178666758,
   'france': 0.0,
   'germany': 21558.2996208357,
   'gold coast': 67781.1426515405,
   'melbourne': 92804.01912347642,
   'paris': 40908.82213277263,
   'perth': 65046.35819797423,
   'sydney': 43786.0579097594}
pop = {'france': 10000, 'china': 202434}

distance_limit = 10
selected_countries = [country for country, distance in distance_dict.items()
                      if distance > distance_limit]
selected_populations = [pop.get(country) for country in selected_countries 
                        if pop.get(country)]


>>>selected_populations
[202434]
于 2013-06-04T00:30:00.590 回答