0

on txt file containing the fallawing:

Tudo Bom;Static and Ben El Tavori;5:13;
I Gotta Feeling;The Black Eyed Peas;4:05;
Instrumental;Unknown;4:15;
Paradise;Coldplay;4:23;
Where is the love?;The Black Eyed Peas;4:13;

i'm trying to use this simple program:

def my_mp3_playlist(file_path):
    with open(file_path, 'r') as playlist_file:
        result_playlist_tuple = tuple()  # Result tuple
        playlist_splitted_lines = playlist_file.readlines()#.split("\n")  # List of songs and additional data
        len_of_playlist_splitted_lines = len(playlist_splitted_lines)  # Getting list length instead of running a
        # loop len(x) times
        playlist_items = list()
        for line in range(len_of_playlist_splitted_lines):  # Extracting list of items from list of lines
            playlist_items.append(playlist_splitted_lines[line].split(';'))
        for element in playlist_items:  # Deleting empty strings '' in playlist_lines
            del element[-1]
            del playlist_items[-1]
        max_song_length, longest_song_name = 0, ""
        max_artist_appearances, most_appearing_artist = 0, ""
        for i in playlist_items:
            # Getting max song length
            curr_float_song_length = float(i[-1].replace(':', '.'))
            if curr_float_song_length > max_song_length:  # Getting max song length
                max_song_length = curr_float_song_length
                longest_song_name = i[0]
            if i.count(i[1]) > max_artist_appearances:  # Getting Most appearing song
                max_artist_appearances = playlist_items.count(i)
                most_appearing_artist = i[1]
        result_playlist_tuple += (longest_song_name, len(playlist_items), most_appearing_artist)
        return result_playlist_tuple
 
 
def main():
    file_path = input("Enter file path: ")
    print(my_mp3_playlist(file_path))
 
 
if __name__ == '__main__':
    main()

      

program need to return tuple: (str:longest_song, int:playlist_items(lines), str:most_appearing_artist)

output i want: ("Tudo Bom", 5, "The black Eyed Peas")

otput i get: ('Tudo Bom', 3, 'Static and Ben El Tavori')*

please help, thanks ahead, Eyal.

2 Answers2

1

As @JMCampos already answered, for every element in the playlist you delete the latest one in the list:

for element in playlist_items:
    del element[-1]
    del playlist_items[-1]

So your list of items is halved and consists of only 3 lines instead of 5. You need to remove this line from the cycle like this:

del playlist_items[-1]
for element in playlist_items:
    del element[-1]

But more pythonic way to ensure you have non-empty data is using list comprehensions like this:

playlist_items = [item for item in playlist_items if item]

The third result parameter is the most appearing artist. You try to count the number of artist appearances in a line which is always 1. And after the check you calculate the maximun number of appearances as the number of appearances of the whole line in the list (which is always 1, too, as you have no duplicate lines in the file):

if i.count(i[1]) > max_artist_appearances:
    max_artist_appearances = playlist_items.count(i)

There are several ways to get the most frequent value from the list. For example you can collect your artist and their number of songs in the dictionary and then get the artist with the highest number:

artists = {}
for item in playlist_items:
    artist = item[1]
    if artist not in artists:
        artists[artist] = 0
    artists[artist] += 1
most_appearing_artist, number_of_appearances = max(artists.items(), key=lambda x: x[1])
Sergey Shubin
  • 3,040
  • 4
  • 24
  • 36
0

I think it might be something regarding this line

        del playlist_items[-1]

You might be trying to delete the last line but you are deleting a line, for every line still in the playlist_items list.

JMCampos
  • 636
  • 1
  • 9
  • 21