簡體   English   中英

從列表中抓取數據,返回最大值和相關數據

[英]scraping data from a list returning max 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'],...]

從上面我想找到最東部和最北部的城市。 所以我想解析找到最大經度或緯度並返回城市和國家。 以下是我的第一次努力,顯然不起作用,因為我不明白如何解決問題。 我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]))

我認為問題在於lambda排序。 您的緯度和經度是字符串,而不是整數或浮點數。 例如,將長度排序為字符串:

[
 '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 = 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]))

這是一個更快的算法,我認為它看起來也更簡單。

你的正確代碼:

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]))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM