![]() |
Problem with comparison operator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem with comparison operator (/thread-16456.html) |
Problem with comparison operator - mdrisser - Feb-28-2019 I'm writing a program that converts a blocklist into a BIND9 zones file for a blackhole/sinkhole DNS server. Within the script I have code similar to the following: # The following list is just for the sake of illustration, the actual list comes from reading the blocklist file my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"] for i in my_list: if(i != "[Adblock]"): print("This block should only execute if i is NOT equal to [Adblock].")The issue is that the if block ALWAYS executes, whether i is equal to [Adblock] or not. I've tried escaping the brackets ("\[Adblock\]") but that didn't help either. I also tried single quotes and single quotes while escaping the brackets. Any help would be greatly appreciated. RE: Problem with comparison operator - Yoriz - Feb-28-2019 It works OK, all I've added is an else # The following list is just for the sake of illustration, the actual list comes from reading the blocklist file my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"] for i in my_list: if(i != "[Adblock]"): print("This block should only execute if i is NOT equal to [Adblock].") else: print("This block should only execute if i is equal to [Adblock].")Outputs
RE: Problem with comparison operator - mdrisser - Feb-28-2019 Thank you for looking into this Yoriz. I haven't tried an else there. What I found that did work (and it contains an else ![]() # The following list is just for the sake of illustration, the actual list comes from reading the blocklist file my_list = ["foo.com/index.html", "bar.com/downloads/malware.exe", "conteso.com/phishing/email.htm", "domain.com/blog/test", "[Adblock]", "another.domain.com/another/test/blog", "blah.net/blah/blah/blah"] for i in my_list: if(re.match('\[Adblock\]', i)): next else: print("This should print only if i is NOT equal to [Adblock].") |