Clearing data from a list returning the maximum value and associated data

[['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'],...]

From the foregoing, I want to find the most eastern and northern cities. Therefore, I want to make out the maximum longitude or latitude and return the city and country. The following is my first effort, which obviously does not work, because I do not understand how to approach the problem. I'm 66, not homework, just trying to figure out if my brain works! Thanks for any help

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]))
+4
source share
3 answers

I think the problem is lambda sorting. You latitude and longitude are strings, not ints or floats. For example, sorted longitudes as a string:

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

So try the following:

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

Output

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

, .

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

:

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

Source: https://habr.com/ru/post/1686472/


All Articles