简体   繁体   中英

Using the Sorted() Function

I made a text file that looks like this:

Houston 69.7 degrees F 2,144,491
Hialeah 77.9 degrees F 217,141
Miami 77.3 degrees F 404,048
Carol City 77.3 degrees F 59,443
North Westside 77.3 degrees F 101,285

And I'm trying to sort the entries by temperature from least to greatest. This is my attempt so far. mf2 is the file that contains my list and mf3 is the file that I'm writing to.

h = aline.find('degrees')
for aline in sorted(mf2, key=lambda aline: aline[(h-5):(h-1)], reverse=False):
    mf3.write(aline)
mf3.close()

That code keeps returning a list that looks like this:

Houston 69.7 degrees F 2,144,491
Hialeah 77.9 degrees F 217,141
Miami 77.3 degrees F 404,048
Carol City 77.3 degrees F 59,443
Miramar 76.9 degrees F 108,072
Tamiami 76.8 degrees F 54,788

I've been banging my head on this for hours and I don't know why it won't sort right. Suggestions?

假设每一行的格式name temperature "degrees F" some-number ,你可以试试

sorted (mf2, key = lambda x: float (x.split (' ') [-4] ) )
data=["Houston 69.7 degrees F 2,144,491",
"Hialeah 77.9 degrees F 217,141",
"Miami 77.3 degrees F 404,048",
"Carol City 77.3 degrees F 59,443",
"North Westside 77.3 degrees F 101,285"]

city=[]
temperature=[]
measure=[]
counts=[]

for s in data:
    #print(s)
    all_matches = re.findall(r'([a-zA-Z]+\s*[a-zA-Z]*)|([0-9.,]+)',s)
    city.append(all_matches[0][0])
    temperature.append(all_matches[1][1])
    measure.append(all_matches[2][0])
    counts.append(all_matches[3][1])


    df=pd.DataFrame({'city':city, 'temperature':temperature, 'measure':measure, 'counts':counts})
    df['temperature']=df['temperature'].astype('float')
    df['counts']=df['counts'].apply(lambda row: int(row.replace(',','')))
    print(df.sort_values(by='counts', ascending=False))

output:

            city  temperature    measure   counts
0        Houston          69.7  degrees F  2144491
2          Miami          77.3  degrees F   404048
1        Hialeah          77.9  degrees F   217141
4  North Westside         77.3  degrees F   101285
3      Carol City         77.3  degrees F    59443

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM