I have a question, I tried to search the forum and see if there is something that can help me but unfortunately I couldn't so here it is: Currently I'm using our AD system and check through that list to see if a user exists and depending on that give them access or not
users = [AD user list]
for user in users:
if user == "user x":
print "you are in"
else:
print "denied"
This works great and all but I want to know if I can make this a bit more efficient, so say if the "user x" is the 10th in the list, this code will print "denied" 9 times before it prints "you are in".
Is it possible to somehow look into the list first and if the string match is in there give a 1 and if not a 0? This way when I print this I get either a 1 or a 0.
Hope my question was clear and I provided enough details. I appreciate all the help.
You can do this using the in
operator. For example:
users = ['john', 'adam', 'susan']
exists = 'john' in users # will be true
exists2 = 'robert' in users # will be false
# for 1 or 0 output
exists = int(exists)
exists2 = int(exists2)
Im pretty sure you can use .index here instead of a full for loop.
try:
users.index('user x')
print 'You are in!' # assuming you are using python 2
catch ValueError:
print 'Denied!'
Using in
is also an option.
if 'user x' in users:
print 'You are in!'
else:
print 'Denied!'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.