How to use previous created list (from file) values to code, printing min and max value?

0

Witam! Jak w temacie, posiadam kod ktory znajduje najmniejsza i najwieksza wartosc z listy jednak jedynie w przypadku gdy wpisze ja recznie do pliku w formacie:
lista = [1,3,5,7,10]...


 minimum = min(lista, key=lambda x: int(x.split(':')[0])*60 + int(x.split(':')[1]))
maximum = max(lista, key=lambda x: int(x.split(':')[0])*60 + int(x.split(':')[1])) 

Chcialbym zeby dane byly pobrane z listy utworzonej wczesniej z pliku ;text_albums_data.txt


open_file = open("text_albums_data.txt").readlines()

# create list in list from data from text_albums_data.txt
lista = []
for line in open_file:
    lista.append(line.upper().rstrip("\n").split(","))

Przykladowa zawartosc pliku txt :

Pink Floyd,The Dark Side Of The Moon,1973,progressive rock,43:00
Britney Spears,Baby One More Time,1999,pop,42:20
The Beatles,Revolver,1966,rock,34:43 (...)

1

Tak Ładujesz plik do listy:

with open(f_name) as f:
	lst = f.readlines()
lst = [x.strip() for x in lst]

print(lst)

A dalej, to co tam Masz za funkcje Możesz stosować.

0

Chcialbym zeby program:

lista = [1:11,3:35,5:43,7:15,10:01]


 minimum = min(lista, key=lambda x: int(x.split(':')[0])*60 + int(x.split(':')[1]))
maximum = max(lista, key=lambda x: int(x.split(':')[0])*60 + int(x.split(':')[1])) 

print(maximum)
print(minimum)

Pobieral dane z pliku tekstowego i wyswietlil najdluszy i najkrotszy w tym przypadku album, tj nie tylko dlugosc ale tez info o wykonawcy itp.
reszta kodu robi cos innego, jest jeszcze tylko utworzenie listy - listnew = []

Kod calego programu:

open_file = open("text_albums_data.txt").readlines()

# create list in list from data from text_albums_data.txt
lista = []

for line in open_file:
    lista.append(line.upper().rstrip("\n").split(","))
#print(lista)


# function find_albums_by_genre
listanew = []


def find_albums_by_genre(genre):
    i = 0
    for i in range(len(lista)):
        for element in lista[i]:
            if element == genre:
                listanew.append(lista[i][1])
                i += 1
    return listanew

# function find_albums_by_artist:
lista_name_albums = []


def find_albums_by_artistname(name):
    for element in lista:
        if element[0] == name:
            lista_name_albums.append(element[1])
        else:
            if name in element[0]:
                lista_name_albums.append(element[1])
    return lista_name_albums


#function find_albums_by_album_name
lista_albums = []


def find_albums_by_album_name(album):
    for element in lista:
        if element[1] == album:
            lista_albums.append(element[1])
        else:
            if album in element[1]:
                lista_albums.append(element[1])
    return lista_albums

#function exit
def end():
   print("THANK YOU ;) SEE YOU SOON")
   exit()

#function back to manu or exit:
def back_menu_or_exit():
    print("----------------------------------------------------------")
    print("WHAT NOW? PRESS:'m' - MENU or 'q' - EXIT")
    next_choice = input("What's your choice?:")
    next_choice = next_choice.lower()
    if next_choice == "m":
        mainmenu()
    else:
        end()

# function find album by time range:


list_year = []
list_time_year = []
lista_final_year = []


def find_albums_by_time_range():
    i = 0
    for i in range(len(lista)):
        list_year.append(int(lista[i][2]))
        i += 1

    print("ENTER YOUR TIME RANGE")
    start_year = int(input("WHAT IS START YEAR?:")) -1
    finish_year = int(input("WHAT IS FINISH YEAR?:")) + 1
    print("----------------------------------------------------------")
    

    for element in list_year:
        if element > start_year:
            if element < finish_year:
                list_time_year.append(str(element))

    return list_time_year

#start program:

print("----------------------------------------------------------")

print("Welcome to Advanced Music Library")
print("----------------------------------------------------------")

def mainmenu():
    del lista_albums[0:]
    del lista_name_albums[0:]
    del listanew[0:]
    del list_year[0:]
    del list_time_year[0:]
    del lista_final_year[0:]

    print("Please make your selection:\n")

    choice = input(
        "aa - to view All imported albums\n"
        "ag - to find All albums by genre\n"
        "at - to find All albums From given time Range\n"
        "sl - to find shortest/longest album\n"
        "ac - to find All albums created by given artist\n"
        "an - to find album by album name\n"
        "rs - to get full report In form of Set of given statistics\n"
        "q  - to exit\n\n"
        "What's your choice?:")
    choice = choice.lower()

    print("----------------------------------------------------------")

    if choice == "aa":
        print("ALL IMPORTED ALBUMS:")
        print("----------------------------------------------------------")
        i = 0
        for i in range(len(lista)):
            print(lista[i][1])
        back_menu_or_exit()

    if choice == "ag":
        print("ENTER GENRE:")
        print("----------------------------------------------------------")
        choice_of_genre = input(
        "pr - progressive rock\n"
        "p - pop\n"
        "r - rock\n"
        "hr - hard rock\n"
        "a - ancient\n"
        "hh - hip hop\n\n"
        "What's your choice?:")
        print("----------------------------------------------------------")
        choice_of_genre = choice_of_genre.lower()
        
        if choice_of_genre == "pr":
            print("ALL ALBUMS OF PROGRESSIVE ROCK:")
            print("----------------------------------------------------------")
            genre = "progressive rock"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1
            
        
        if choice_of_genre == "p":
            print("ALL ALBUMS OF POP:")
            print("----------------------------------------------------------")
            genre = "pop"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1
        
        if choice_of_genre == "r":
            print("ALL ALBUMS OF ROCK:")
            print("----------------------------------------------------------")
            genre = "rock"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1
            
        if choice_of_genre == "hr":
            print("ALL ALBUMS OF HARD ROCK:")
            print("----------------------------------------------------------")
            genre = "hard rock"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1
            
        if choice_of_genre == "a":
            print("ALL ALBUMS OF ANCIENT:")
            print("----------------------------------------------------------")
            genre = "ancient"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1

        if choice_of_genre == "hh":
            print("ALL ALBUMS OF HIP HOP:")
            print("----------------------------------------------------------")
            genre = "hip hop"
            genre = genre.upper()
            find_albums_by_genre(genre)
            i = 0
            for i in range(len(listanew)):
                print(listanew[i])
                i += 1
                
        back_menu_or_exit()

    if choice == "ac":
        print("ENTER NAME OF ARTIST:")
        print("----------------------------------------------------------")
        name = input("What's your choice?")
        name = name.upper()
        print("----------------------------------------------------------")
        print("ALL ALBUMS OF THIS ARTIST:")
        print("----------------------------------------------------------")
        find_albums_by_artistname(name)
        i = 0
        for i in range(len(lista_name_albums)):
            print(lista_name_albums[i])
            i += 1
        back_menu_or_exit()

    if choice == "an":
        print("ENTER NAME OF ALBUM:")
        print("----------------------------------------------------------")
        album = input("What's your choice?")
        album = album.upper()
        print("----------------------------------------------------------")
        print("ALL WHAT I FOUND:")
        print("----------------------------------------------------------")
        find_albums_by_album_name(album)
        i = 0
        for i in range(len(lista_albums)):
            print(lista_albums[i])
            i += 1
        back_menu_or_exit()
    
    if choice == "at":
        find_albums_by_time_range()
        i = 0
        for i in range(len(lista)):
            for element in lista[i]:
                if element == element in list_time_year:
                    lista_final_year.append(lista[i][1])
                    i += 1
        #print(lista_final_year)
        i = 0
        for i in range(len(lista_final_year)):
            print(lista_final_year[i])
            i += 1
        back_menu_or_exit()

    if choice == "q":
        end()

mainmenu()
0

Czyli musisz zaimplementować parsowanie pliku.
Mając plik w takiej formie:
Pink Floyd,The Dark Side Of The Moon,1973,progressive rock,43:00
Britney Spears,Baby One More Time,1999,pop,42:20
można zrobić taki myk. Dzięki zapisowi z "with" zabezpieczamy się w razie problemów z pikiem (np. brakiem pliku) oraz nie musimy pamiętać o zwolnieniu handle do pliku:

def parse_timestamp(timestamp_raw):
    minutes, seconds = timestamp_raw.split(':')
    return int(minutes)*60 + seconds
for line in open("text_albums_data.txt"):
    artist, album, year, genre, timestamp_raw = line.split(',')
    timestamp = parse_timestamp(timestamp_raw)

Na tej podstawie stwórz klasy Artist, Album itp i przechowuj tam sparsowane informacje. Dzięki temu jak będziesz wyszukiwać najdluższy/najkrótszy album będziesz mógł wydrukować informacje. Oczywiście jest to dobry moment aby przemyśleć relacyjną bazę danych. Jeden album może mieć wiele wykonawców, jeden artysta wiele albumów itp..

1 użytkowników online, w tym zalogowanych: 0, gości: 1