Python Forum
If statement not separating Albums
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement not separating Albums
#1
If statement does not separate between Albums from List. When ran it all prints Yes and when I used not equal all printed no. Why is it not checking to see if the Album is NT and print Yes and all others print No?

newtracks = List of updated file names from folder, filezto = List that was used to update files which is list newtracks. Album=List of Albums(OT and NT Only). With the exception of the list newtracks all were taken from a spreadsheet. What I am seeking to accomplish is to have it compare the files(newtracks) and (filezto) and check if the album is NT and if so print Yes else print No. The other last piece of the puzzle is to move those files that match to selected folders based upon a list from the spreadsheet . for ex file 01 Gen00 album: NT move to folders 1-GEN Gene, etc.

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', newtracks[nt] ,' ft', filezto[ft])

                else:
                        print('No', newtracks[nt], 'ft',filezto[ft])
                nt = nt +1
                ft = ft +1
Reply
#2
What is ft, nt, alb, filezto?

Please describe in words what you want to do and post some example data.

One very non-pythonic pattern is: for x in range(len(sequence))

Better is:
for index, element in enumerate(sequence):
    print(index, element)
And if element is not required:
for index, _ in enumerate(sequence):
    print(index, element)
The _ is a throw away name (variable).

And if you just to repeat something, where index and the element is not needed:
for _ in range(len(sequence))
    print("Working...")
I like more the enumerate-solution. Looks nicer. Lesser parenthesis.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Feb-26-2021, 10:05 AM)DeaD_EyE Wrote: And if you just to repeat something, where index and the element is not needed:
for _ in range(len(sequence))
    print("Working...")
I like more the enumerate-solution. Looks nicer. Lesser parenthesis.

Alternative to this could be iterate directly over items:

for _ in sequence:
   print('Working...')
DeaD_EyE likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
You're right.
My example was also not pythonic.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
#6
Thanks for the help. I did not want to over explain as it may become over explained. Below is a little more details.

Lists:

[inline]newtracks = ['(01 GEN 000) - Intro To Genesis.mp3','(01 GEN 001) - Genesis 1.mp3','(02 EXO 000) - Intro To Exodus.mp3','(02 EXO 001) - Exodus 1.mp3',etc] This list contains the songs from folder
[/inline]
filezto = Files To ['(01 GEN 000) - Intro To Genesis.mp3','(01 GEN 001) - Genesis 1.mp3','(02 EXO 000) - Intro To Exodus.mp3','(02 EXO 001) - Exodus 1.mp3',etc] List that updated list newtracks from spreadsheet.

albm = ['NT', 'OT'] # In the spreadsheet they are only 2 Albums.

ordfldrtosrt = order of folder to sort contains values for the subfolders for the tracks for album OT

fortrfnew = For Transfer New contains values for the subfolders for the tracks for album NT

ft = File to: It was used to updated the file names in list newtracks
nt = Is one of the 2(OT/NT) Albums in the spreadsheet.

I have spreadsheet that I want to use that has all of values needed. What I will like to do is match the songs in list newtracks with filezto and if their album is NT copy the files to folder> NT> Subfolders: are in list ordfldrtosrt and if album OT copy them to folder> OT> subfolders: are in list[fortrfnew]

In Column A: They are Two Albums(OT/NT): List is albm
In Column N: The File Names that updated the songs: List newtracks

while nt < len(newtracks):
    while ft < len(filezto):
        for alb in range(len(albm)):
                if not albm[alb] == 'NT' and newtracks[nt] == filezto[ft] :

                        print('Yes', newtracks[nt] ,' File To', filezto[ft])
                        #os.shutil()
                else:
                        print('No', newtracks[nt], 'File To',filezto[ft])
                        #os.shutil()
                nt = nt +1
                ft = ft +1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error getting tracks from albums ozzyk 7 4,487 Nov-22-2017, 07:33 PM
Last Post: snippsat
  Getting top albums from last.fm ozzyk 1 2,980 Nov-19-2017, 09:40 PM
Last Post: heiner55
  Separating a string into evenly sized elements in a list kennybassett 9 6,242 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