Python 从返回最大值和关联数据的列表中删除数据
从上面我想找到最东部和最北部的城市。所以我想解析最大经度或纬度,然后返回城市和国家。以下是我的第一次努力,但显然不起作用,因为我不知道如何解决这个问题。我66岁了,所以没有做作业,只是想看看我的大脑是否还能工作!谢谢你的帮助Python 从返回最大值和关联数据的列表中删除数据,python,python-3.x,Python,Python 3.x,从上面我想找到最东部和最北部的城市。所以我想解析最大经度或纬度,然后返回城市和国家。以下是我的第一次努力,但显然不起作用,因为我不知道如何解决这个问题。我66岁了,所以没有做作业,只是想看看我的大脑是否还能工作!谢谢你的帮助 [['Albania', 'Tirana', '41.3289', '19.8178'], ['Andorra', 'Andorra la Vella', '42.5000', '1.5000'], ['Austria', 'Vienna', '48.2000', '1
[['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'],...]
我认为问题在于lambda排序。纬度和经度是字符串,而不是整数或浮点数。例如,将长度排序为字符串:
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.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
不需要对列表进行排序就可以找到larges元素
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]))
这是一个更快的算法,我认为它看起来也更简单。谢谢您的帮助。这似乎适用于格子,但不适用于经度,从完整的列表中,我认为赫尔辛基是最北部,但我认为卢森堡是最东部的wheras,应该是布加勒斯特(我的列表中没有塞浦路斯,它应该是)。我相信这是我的错误,我的想法很有效。非常感谢。我可以在网上做一些练习来“掌握”排序吗?我不知道关于排序的确切情况,但我学过python。也许这就是你需要的。谢谢。我也要试试这个。
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]))