user3339656
user3339656

Reputation: 3

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?

Upvotes: 0

Views: 67

Answers (2)

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

Upvotes: 0

Hyperboreus
Hyperboreus

Reputation: 32429

Supposing that each line is of the format name temperature "degrees F" some-number, you could try

sorted (mf2, key = lambda x: float (x.split (' ') [-4] ) )

Upvotes: 4

Related Questions