0
def findSongs(jukeBox):
    search = input('Enter a word you want to search in the dataset:')
    cnt=0
    for artist in jukeBox:
        artist = artist.strip().split(',')
        if search.lower() in ''.join(artist).lower():
            cnt+=1
    print('\n\nFound' , cnt, 'matches:\n------------------------\n')
    for artist in jukeBox:
        artist = artist.strip().split(',')
        if search.lower() in ''.join(artist).lower():
            printSong(artist[0],artist[1],artist[2],artist[3])

I have code that will search a list for terminal input, I would like to store the matching data to a list so that I do not have to search once to get a count and search again to print.

Marsilinou Zaky
  • 998
  • 4
  • 16
Terry
  • 11
  • 1

2 Answers2

1

Just store matching artists in a list:

def findSongs(jukeBox):
    search = input("Enter a word you want to search in the dataset:")
    matching_artists = []
    for artist in jukeBox:
        artist = artist.strip().split(",")
        if search.lower() in "".join(artist).lower():
            matching_artists.append(artist)
    print("\n\nFound", len(matching_artists), "matches:\n------------------------\n")
    for artist in matching_artists:
        print(artist[0], artist[1], artist[2], artist[3])
RafalS
  • 3,464
  • 10
  • 20
  • Thanks RafalS! this is what I wanted. for some reason I was not cycling through the list to print. – Terry Dec 08 '19 at 16:01
0

You're trying to implement what's called by Memoization, it's a good way to keep track of historical results to reduce the computation and speed up the program. Here's a sample code on how to get started

history = dict()

def findSongs(jukeBox):
    search = input('Enter a word you want to search in the dataset:')
    cnt=0
    if search in history.keys():
        # The search term exists, to access the previous result -
        # - from the dict: history[search]
    else:
        for artist in jukeBox:
            artist = artist.strip().split(',')
            if search.lower() in ''.join(artist).lower():
                cnt+=1
        print('\n\nFound' , cnt, 'matches:\n------------------------\n')
        for artist in jukeBox:
            artist = artist.strip().split(',')
            if search.lower() in ''.join(artist).lower():
                printSong(artist[0],artist[1],artist[2],artist[3])
                # Makre sure to update the dictionary here 
                # history[search] = [results list]
                # or history[search] = history[search].append(newElement)
Marsilinou Zaky
  • 998
  • 4
  • 16
  • 1
    @Terry The odds of receiving useful information are much higher when the question extends beyond “I want to do this and I don’t know how.” The fact you even got an answer, never mind two, is a miracle unto itself. – AMC Dec 08 '19 at 10:47