对于人口
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中是否可以实现?
答案 0 :(得分: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]