Python Forum
If statement not separating Albums
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement not separating Albums
#5
In your effort to produce a short example you produced code that doesn't run at all. I created some lists and tried your code.
newtracks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 19]
filezto = [1, 3, 5, 7, 9]
albm = ['NT', 'OT', 'NT', 'OT']
nt = 0
ft = 0
 
for alb in range(len(albm)):
    while nt < len(newtracks):
        while ft < len(filezto):
            if albm[alb]  == 'NT' and newtracks[nt] == filezto[ft] :
                    print('Yes', alb, nt, ft)
            else:
                 print('No', alb, nt, ft)
            nt = nt +1
            ft = ft +1
print('Done')
Output:
Yes 0 0 0 No 0 1 1 No 0 2 2 No 0 3 3 No 0 4 4
Notice the absense of "Done"? Both nt and ft start at 0 and both are incremented each time through the "while ft < len(filezto)" loop. This results in nt and ft always being the same, and that results in an infinite loop if newtracks is longer than filezto.

So I change when nt and ft are incremented.
for alb in range(len(albm)):
    while nt < len(newtracks):
        while ft < len(filezto):
            if albm[alb]  == 'NT' and newtracks[nt] == filezto[ft] :
                    print('Yes', alb, nt, ft)
            else:
                 print('No', alb, nt, ft)
            ft = ft +1
        nt = nt +1
print('Done')
Output:
Yes 0 0 0 No 0 0 1 No 0 0 2 No 0 0 3 No 0 0 4 Done
Now the code runs to completion, but it only processes albm[0] and newtracks[0]. The reason for this is we never reset the loop counters to zero, and when we try to process newtracks[1], ft == len(filezto) and the innermost loop doesn't run. I moved where the ft and nt variables are initialized.
newtracks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
filezto = [1, 3, 5, 7, 9]
albm = ['NT', 'OT', 'NT', 'OT']
 
for alb in range(len(albm)):
    nt = 0
    while nt < len(newtracks):
        ft = 0
        while ft < len(filezto):
            if albm[alb]  == 'NT' and newtracks[nt] == filezto[ft] :
                    print('Yes', alb, nt, ft)
            else:
                 print('No', alb, nt, ft)
            ft = ft +1
        nt = nt +1
print('Done')
This code tests all the conditions. The output goes on for 200 lines! 4 albm x 10 newtracks * 5 filezto. I don't understand your logic, but I don't think this is the output you are looking for. Because I didn't understand the comparison logic at all I changed things around a bit to a form I'm more familiar with (I don't like while loops).
for alb in albm:
    if alb == 'NT':
        for nt in newtracks:
            for ft in filezto:
                if nt == ft:
                        print('Yes', alb, nt, ft)
                else:
                    print('No', alb, nt, ft)
    else:
        print('No', alb)
Your comparison loops through newtracks and filezto even if alb != 'NT' even though the result is guaranteed to be "No", so I split the comparison into "if alb == 'NT'" and "if nt == ft". This printed half as many results (since half of my albums are "NT"). But I don't think they are what you are looking for. For example, this is the first 5 lines of output.
Output:
Yes NT 1 1 No NT 1 3 No NT 1 5 No NT 1 7 No NT 1 9
The newtracks[0] (1) is compared to each value of filezto, and Yes or No is printed for each comparison. I think you want newtracks[0] to be compared to every value in filezto and print 'Yes' if it matches any of them, and 'No' if it matches none. So I tried that logic.
for alb in albm:
    if alb == 'NT':
        for nt in newtracks:
            for ft in filezto:
                if nt == ft:
                        print('Yes', alb, nt)
                        break
            else:
                print('No', alb, nt)
    else:
        print('No', alb)
Output:
No NT 4 Yes NT 5 No NT 6 Yes NT 7 No NT 8 Yes NT 9 No NT 19 No OT Yes NT 1 No NT 2 Yes NT 3 No NT 4 Yes NT 5 No NT 6 Yes NT 7 No NT 8 Yes NT 9 No NT
My newtracks was a list of counting numbers from 1 to 10. My filzto was a list of odd numbeers in the range 1 to 10. So Yes is printed if the newtrack is odd and No if the newtrack is even, and only one result is printed for each newtrack.

If those are the results you want, you can replace the filzto loop with "if nt in filzto".
for alb in albm:
    if alb == 'NT':
        for nt in newtracks:
            if nt in filezto:
                print('Yes', alb, nt)
            else:
                print('No', alb, nt)
    else:
        print('No', alb)
If this is not what you are looking for then I apologize for wasting your time.
Reply


Messages In This Thread
If statement not separating Albums - by giddyhead - Feb-26-2021, 09:48 AM
RE: If statement not separating Albums - by deanhystad - Feb-26-2021, 02:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  error getting tracks from albums ozzyk 7 4,639 Nov-22-2017, 07:33 PM
Last Post: snippsat
  Getting top albums from last.fm ozzyk 1 3,045 Nov-19-2017, 09:40 PM
Last Post: heiner55
  Separating a string into evenly sized elements in a list kennybassett 9 6,459 Jun-14-2017, 08:22 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020