0

My program 'technically' works... but there's gotta be a better solution.

So there is a list of top 500 movies in random orders on a text file.

Avatar    71245151
Star Wars    92815125
Matrix    4215151  ......

The question is to create a function that takes the text file as an input and write the top 500 movies in order (Highest to Lowest Sales) to another file.

def sort_films(file_name, destination_file):
    open_file = open(file_name, "r")    #Read the file
    movie_list = []

    for line in open_file:
       movie_list.append(line.split("\t")) #Since "\t" exists, split

    open_file.close()    

Here, movie_list would look something like this

movie_list = [['Avatar', '5241521\n'], ['Star Wars', '9512512'], ....]

Since we don't want line breaks when converting the string of numbers into integers and sorting the numbers from high to low, this is what I did. I also put the numbers in the front of each list because sort() sorts the movies in alphabetical order which is not what I want.

    for movie in movie_list:
        movie.insert(0, movie[1])
        movie.pop()
    for movie in movie_list:
        movie[0] = int(movie[0].replace("\n", ""))


    movie_list.sort(reverse = True)

Now I'm going to write the files.

    open_file = open(destination_file, "w")

    string = ""

I added a line break because we want to display the movie just how it was like in the other text file (after converting the sales into a string.)

Changed the position because the order was [Movie, Sales] initially.

    for movie in movie_list:
        movie[0] = str(movie[0]) + "\n" 
        movie.insert(0, movie[1])
        movie.pop()

Then we had the "\t" before so I joined the name of the movie and the sales.

        string += "\t".join(movie)

    open_file.write(string)

    open_file.close()   

sort_films("top500.txt", "top500result.txt")

I feel like there is a way of sorting the number from high to low without having to change the index (position) in the list...

Would be grateful if anyone can help me out.

T. Kim
  • 3
  • 1
  • Use a `dict`, like `ratings = {}; ratings['foo'] = 1234;` then at last `sorted(ratings.items(), key=lambda x: x[1])` should do it :) – han solo Mar 11 '19 at 14:50

4 Answers4

0

movie_list.sort(key=lambda x: x[1])

As exampled here: Sort tuples based on second parameter

eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
0

You sound like you want to be able to sort a list of lists by an element inside of the inner list (sales). Maybe you could try something like this:

movie_list.sort(key=lambda x: x[1])

That should do it

Benny
  • 186
  • 1
  • 1
  • 11
0

I suggest you classes. In your case, you need one class with one string and one integer. From that point, everything will go on easier. You will have only one array full of objects, then you can play with objects as you like (sort them, pop, insert etc).

0

Like other users already mentioned you can use sort in combination with a lambda function to sort by the second element of a tuple. To avoid splitting your textfile by tabs and replacing all newline characters I'd suggest to use a csv reader:

import csv
def sort_films(file_name, destination_file):
    movie_list = []
    with open(file_name, 'r') as csvfile:
        # open csv file with delimiter '\t'  to avoid splitting your string
        csv_reader = csv.reader(csvfile, delimiter='\t')
        for row in csv_reader:
            movie_list.append((row[0], int(row[1]))) # cast string to int, so it gets sorted correctly

    # sort your list by the second element of your tuple by using labda function
    movie_list_sorted = sorted(movie_list, key=lambda x: x[1], reverse=True)

    # save your sorted list into the destination_file
    with open(destination_file,'w') as f:
        for row in movie_list_sorted:
            # while saving you can use an other format by replacing \t with whatever you want
            f.write('%s\t%s\n' % (row[0], row[1]))

    sort_films('top500.txt', 'sorted.txt')
SerAlejo
  • 473
  • 2
  • 13