【发布时间】:2020-07-06 11:28:25
【问题描述】:
在包含 flawing 的 txt 文件中:
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;
我正在尝试使用这个简单的程序:
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()
程序需要返回元组:(str:longest_song, int:playlist_items(lines), str:most_appearing_artist)
我想要的输出:("Tudo Bom", 5, "The black Eyed Peas")
我得到:('Tudo Bom', 3, 'Static and Ben El Tavori')*
请帮忙, 提前谢谢, 艾尔。
【问题讨论】:
标签: python