Using sets will be screaming fast if you have any volume of data
If you are willing to use sets, you have the isdisjoint() method which will check to see if the intersection between your operator list and your other list is empty.
MyOper = set(['AND', 'OR', 'NOT'])
MyList = set(['c1', 'c2', 'NOT', 'c3'])
while not MyList.isdisjoint(MyOper):
print "No boolean Operator"
http://docs.python.org/library/stdtypes.html#set.isdisjoint
Answer from gahooa on Stack OverflowTrouble understanding While Not
If we assign false to a variable and then use a while not loop, why is the variable still false and not true?
How do while not loops work
Is it weird that I NEVER use while loops? It’s just not something I’ve felt like I needed, ever. What are some situations where using a while loop is necessary?
Videos
Using sets will be screaming fast if you have any volume of data
If you are willing to use sets, you have the isdisjoint() method which will check to see if the intersection between your operator list and your other list is empty.
MyOper = set(['AND', 'OR', 'NOT'])
MyList = set(['c1', 'c2', 'NOT', 'c3'])
while not MyList.isdisjoint(MyOper):
print "No boolean Operator"
http://docs.python.org/library/stdtypes.html#set.isdisjoint
The expression 'AND' and 'OR' and 'NOT' always evaluates to 'NOT', so you are effectively doing
while 'NOT' not in some_list:
print 'No boolean operator'
You can either check separately for all of them
while ('AND' not in some_list and
'OR' not in some_list and
'NOT' not in some_list):
# whatever
or use sets
s = set(["AND", "OR", "NOT"])
while not s.intersection(some_list):
# whatever
I'm doing "automate the boring stuff" and was on an early lesson about while loops. I can't wrap my head around "while not." The code is below
name = ''
while not name:
print('Enter your name:')
name = input()
print('How many guests will you have?')While my limited knowledge, I would write it instead as
name = ''
while name == '':
print('Enter your name:')
name = input()
print('How many guests will you have?')My question is how can they mean the same thing? Both start with name = False/empty. The way I interpret the top code is, while name is not False/empty. The way I interpret the bottom is, while name is equal to False/empty. In my mind, the top would keep looping with any True value. Thanks for your help!
EDIT: formatting
EDIT 2: Finally figured it out. I'm posting this edit for prosperity sake, for any other programming ogres like me. As long as the condition line is True, the loop will run. If the condition is False, the loop will not run. Even though name is False, the condition line is True and the loop will go until something is inputted. Once something is typed in, name becomes True, which would make the condition not name False, ending the loop.
Thanks everyone for helping me understand that! It's so simple now that I see it. I'm super new to Python and made a rookie mistake.
I was reading through this code and I'm just not getting how while loops work when not operator is also used.
https://pastebin.com/5mfBhQSb
I thought the not operator just inversed whatever the value was but I just can't see this code working if that's the case.
For example , the while not sequenceCorrect should turn the original value of False to True, so the loop should run while it's True. But why not just state while True then? And why declare sequenceCorrect = True again? Doesn't it just means while True, make it True? And so on.
The only way it makes sense to me is of the while not loop always means False (just like the while always means as long as it's True) even if the value is supposed be False and should be inverted to True.
So, is that the case? Can anyone explain why it works like that?
I’ve written a lot of python code over the last few months, but I can’t think of a single time I’ve ever used a while loop. I mean, I use for loops pretty often, but not while loops.
Is this just down to personal preference, and I’m just using what I’m comfortable with? Or can you guys think of some situations where a while loop would be the easiest way to do things, but it’s possible to do it with a for loop? Maybe I’m using for loops in situations that I should be using while loops.
EDIT: Thanks for the suggestions. I found a few places in my code where a while loop makes sense.
First, I check if a folder has any files in it, and while it does, I delete the first one in the list:
useless_files = os.listdir("MGF_HR_Data") # I think these are files it creates when downloading the MGF data but they are extra and don't do anything
while len(useless_files)>0:
os.remove(f"MGF_HR_Data/{useless_files[0]}")
useless_files = os.listdir("MGF_HR_Data")
print("Deleting useless file from MGF_HR_Data") I also used a while loop to check if the data has been downloaded, and if it hasn't prompt the user to download it and then press enter to check again. This way, the code doesn't break if the user forgot to download the file first:
# Check if you downloaded the file in Matlab already. If not, ask the user to download it and try again.
while os.path.exists(file1)==False:
print(f"{file1} not found. Please open Matlab and run the following code to download the MGF data:" )
print(f"download({wstime.year}, {wstime.month}, {wstime.day})")
input("Press enter to try again after downloading the MGF data. \n") (Okay, I know this isn't the most efficient way to do this. There must be a way to use python to open matlab and run a matlab command, rather than asking the user to do it themselves. However, I don't know how to do that, so this works for now.)