【问题标题】:Python Assistance Searching a ListPython 帮助搜索列表
【发布时间】:2017-08-22 02:28:39
【问题描述】:
"""read file and store into database"""
f = open('C:\\Users\\user.name\\Desktop\\tunes.txt','r')
artist=[""]
song=[""]
album=[""]
genre=[""]
index=0
for line in f:
    if index==0:
        artist.append(line)
        index=index+1
    elif index==1:
        song.append(line)
        index=index+1
    elif index==2:
        album.append(line)
        index=index+1
    elif index==3:
        genre.append(line)
        index=index+1
    elif index==4:
        index=0   
while 1:
    selection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Search\n2.Recommend\n3.Edit\n4.Save\n"))
    if selection == 1:
        print "You selected Search"
        searchselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Display All Songs\n2.Display All Artists\n3.Search By Artist\n4.Display All Genres\n5.Search by Genre\n6.Display All Playlists\n7.Search By Playlist\n"))                               
        if searchselection == 1:
           print '[%s]' % ''.join(map(str, song))
        elif searchselection == 2:
            print '[%s]' % ''.join(map(str, artist))
        elif searchselection == 3:
            artistsearch = str(raw_input("\nWhat artist are you searching for?\n"))
            artist.index(artistsearch)
            print value
        elif searchselection == 4:
            print '[%s]' % ''.join(map(str, genre))
        elif searchselection == 5:
            print "display"
        elif searchselection == 6:
            print "display"
        elif searchselection == 7:
            print "display"
        break
    elif selection == 2:
        print "You selected recommend."
        recommendselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Recommend by Song Title\n2.Recommend by Artist Name\n"))
        if recommendselection == 1:
            songrec = str(raw_input("Please enter the song title\n"))

        elif recommendselection == 2:
            artistrec = str(raw_input("Please enter the Artist's name\n"))
        break
    elif selection == 3:
        print "You selected edit."
        editselection = int(raw_input("Please select the number that corresponds with what you would like to do.\n1.Add a New Song\n2.Create New Playlist\n3.Add a song to a current playlist"))
        if editselection == 1:
            songadd = str(raw_input("Please enter the EVERYTHING\n"))
        elif editselection == 2:
            playistcreate = str(raw_input("Please enter the name of the Playlist\n"))
        elif editselection == 3:
            playlistadd = str(raw_input("Please enter the name of the playlist\n"))      
        break
    elif selection == 4:
        print "You selected Save."
        f.close()
        break

这就是我目前所拥有的。这是一个正在进行的 python 项目,现在我很难过;我正在尝试按艺术家进行搜索,例如用户将 Justin Timberlake 输入为“artistsearch”,然后我希望提取索引,以便我可以匹配歌曲列表中的索引并将该信息显示给用户。

任何帮助确定为什么 Justin Timberlake 不是列表中的值,即使在我运行显示所有艺术家选项时显示名称,将不胜感激。

同时指出匹配列表索引的正确方向也会很棒。这是 tunes.txt 的一个示例:

Alicia Keys
No One
As I Am
R&B/Soul;Syl tunes

Everything But the Girl
Missing
Amplified Heart
Alternative;Syl tunes

Gathering Field
Lost In America
Lost In America
Rock

Ellie Goulding
Burn
Halcyon
Pop;Road tunes

Justin Timberlake
Mirrors
Mirrors (Radio Edit) - Single
Pop;Syl tunes

【问题讨论】:

  • 听起来list 不是一个合适的数据结构。这将通过字典/嵌套字典大大简化。
  • 你可能是对的,但我已经走到这一步了,不知道我需要做多少回溯和研究来实现字典数据结构。
  • 将上面的.txt文件解析成嵌套字典结构应该比较容易。大概有 5-10 行代码很冗长......

标签: python database list indexing


【解决方案1】:

我认为您应该为要存储的数据创建一个特定的类,然后创建一个实例化该类的对象列表:

class Song():
    """Class to store the attributes for each song"""
    def __init__ (self):
        self.artist = ""
        self.song = ""
        self.album = ""
        self.genre = ""

# List to store all the Song objects
songs_list = []

f = open('C:\\Users\\user.name\\Desktop\\tunes.txt','r')
index=0
for line in f:
    # Instantiate an empty Song object
    s = Song()
    if index == 0:
        s.artist = line
        index=index+1
    elif index == 1:
        s.song = line
        index = index + 1
    elif index == 2:
        s.album = line
        index = index + 1
    elif index == 3:
        s.genre = line
        index = index+1
    elif index == 4:
        index = 0
    songs_list.append(s)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2015-08-25
    相关资源
    最近更新 更多