2017-09-27 37 views
4
[['Albania', 'Tirana', '41.3289', '19.8178'], 
['Andorra', 'Andorra la Vella', '42.5000', '1.5000'], 
['Austria', 'Vienna', '48.2000', '16.3667'], 
['Belarus', 'Minsk', '53.9000', '27.5667'], 
['Belgium', 'Brussels', '50.8500', '4.3500'], 
['Bosnia and Herzegovina', 'Sarajevo', '43.8667', '18.4167'], 
['Bulgaria', 'Sofia', '42.7000', '23.3300'], 
['Croatia', 'Zagreb', '45.8167', '15.9833'], 
['Czech Republic', 'Prague', '50.0833', '14.4167'], 
['Denmark', 'Copenhagen', '55.6761', '12.5683'], 
['Estonia', 'Tallinn', '59.4372', '24.7453'], 
['Finland', 'Helsinki', '60.1708', '24.9375'], 
['France', 'Paris', '48.8567', '2.3508'],...] 

从上面我想找到最东部和最北部的城市。所以我想分析找到最大的经度或纬度,并返回城市和国家。以下是我的第一次努力,显然不起作用,因为我不明白如何解决问题。我66岁,所以不做家庭作业,只是想看看我的大脑是否仍然有效!感谢您的帮助从列表中抓取数据返回最大值和相关数据

def find_city(): 
    from operator import itemgetter 
    eu_list=[] 
    EU =['Austria','Belgium', 'Bulgaria','Czech Republic', 'Croatia', 'Cyprus','Denmark', 'Estonia', 'France', 'Finland', 'Germany', 'Hungary','Ireland', 'Italy', 'Luxembourg', 'Latvia', 'Lithuania', 'Malta','Netherlands', 'Portugal', 'Poland', 'Romania', 'Spain', 'Sweden','Slovakia', 'Slovenia','United Kingdom'] 
    countrylist =parse_doc() 
    #print(countrylist) 

    for item in countrylist: 
     country = item[0] 
     capital = item[1] 
     latitude = item[2] 
     longitude = item[3] 

     if country in EU: 
      eu_list.append(item) 
      #print("{} is the capital of {} and is at {} north and {} east".format(capital,country, latitude, longitude))  
     #else: #item[0] not in EU: 
      #print("{} is the capital of {} and is at {} north and {} east, but is not in the EU".format(capital,country, latitude, longitude))  
    result = sorted(eu_list,key=lambda x: x[3], reverse=True)[3] #doesn't work 
    result1= sorted(eu_list,key=lambda x: x[3], reverse=True)[2] #doesn't work 
    print("{} in {} is the most eastern city in the EU".format(result[1], result[0])) 
    print("{} in {} is the northern-most city in the EU".format(result1[1], result1[0])) 

回答

1

我认为问题在于lambda排序。你的经纬度是字符串,而不是整数或浮点数。例如,分类longtitudes为字符串:

[ 
'1.5000', 
'12.5683', 
'14.4167', 
'15.9833', 
'16.3667', 
'18.4167', 
'2.3508', 
'23.3300', 
'24.7453', 
'24.9375', 
'27.5667', 
'4.3500' 
] 

那么试试这个:

result = sorted(eu_list,key=lambda x: float(x[3]), reverse=True)[0] # longitude sorted 
result1= sorted(eu_list,key=lambda x: float(x[2]), reverse=True)[0] # latitude sorted 

输出

Helsinki in Finland is the most eastern city in the EU 
Helsinki in Finland is the northern-most city in the EU 
+0

效果很好。谢谢。在网上有练习,我可以做“精通”排序吗? – user1478335

+0

我并不完全知道排序,但有一个[游戏](https://checkio.org/),我学习了python。也许这是你需要的。 –

0

您的权利代码:

result = sorted(eu_list,key=lambda x: x[3], reverse=True)[0] #x[3] is the longitude that you want to compare, and the last [0] point to the first element of the sorted list 
result1= sorted(eu_list,key=lambda x: x[2], reverse=True)[0] #x[2] is the latitude that you want to compare 
print("{} in {} is the most eastern city in the EU".format(result[1], result[0])) 
print("{} in {} is the northern-most city in the EU".format(result1[1], result1[0])) 
+0

谢谢你的帮助。这似乎适用于地域性,但不是经度,从整个名单我得到的赫尔辛基北部最多,但我得到卢森堡最东部wheras应该是布加勒斯特(我的名单不包含塞浦路斯,它应该)。我相信它的错误虽然 – user1478335

1

您不需要对列表进行排序来查找larges元素。

result = max(eu_list, key=lambda x: float(x[3])) 
result1 = max(eu_list, key=lambda x: float(x[2])) 
print("{} in {} is the most eastern city in the EU".format(result[1], result[0])) 
print("{} in {} is the northern-most city in the EU".format(result1[1], result1[0])) 

这是一个更快的算法,我认为它看起来也更简单。

+1

谢谢。我也会试试这个。 – user1478335