简体   繁体   中英

How to sort the text in a file in python?

I have a .txt file like this:

ancient 0.4882
detained 5.5512
neighboring 2.9644
scores 0.5951
eggs 0.918
excesses 3.0974
proceedings 0.7446
menem 1.7971

I want to display the top 3 words by comparing their value in one list and the remaining words in another list.

ie, the output for this example should be:

[detained, excesses, neighboring] & [menem, eggs, proceedings, scores, ancient]

How to do that?

EDIT:

I forgot to mention one thing: I want to consider only those words that have a value great than 0.5 How to do that?

import csv    
with open('inputFile.csv','r') as inputFile:
    reader = csv.reader(inputFile, delimiter = " ")    
    word = dict()    
    for line in reader:
        if float(line[1]) > 0.5:
            word[line[0]] = float(line[1])

    sortedArray = sorted(word.iteritems(), key=lambda x:-x[1])
    maxWords = sortedArray[:3]
    Remaining = sortedArray[3:]    
    print maxWords
    print Remaining
import csv
with open('x.txt') as f:
    # use space as delimiter
    reader = csv.reader(f, delimiter=' ')
    # sort by the value in the second place of each line i.e. x[1]
    s = sorted(reader, key=lambda x: x[1], reverse=True)
    # filter only grater than 0.5 and take the first value only
    l = [x[0] for x in s if float(x[1])>0.5]
    print l[:3]
    print l[3:]

The answers using csv are more concise than mine but here is another approach.

from operator import itemgetter

with open('file_list_data.txt', 'r') as f:
    lines = f.readlines()

records = [l.split() for l in lines]
records_with_numbers = [(r[0], float(r[1])) for r in records if float(r[1]) > 0.5]

sorted_records = sorted(records_with_numbers, key=itemgetter(1), reverse=True)

top_3 = [word for (word, score) in sorted_records[0:3]]
rest = [word for (word, score) in sorted_records[3:]]

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