comparison

Python None comparison: should I use "is" or ==?

Python None comparison: should I use "is" or ==? Question: My editor warns me when I compare my_var == None, but no warning when I use my_var is None. I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None is …

Total answers: 7

Python – Compare files

Python – Compare files Question: I need to use a get() to select an object by comparing it with a FILE request: My models: class Work (models.Model): file = models.FileField(storage=OverwriteStorage(), upload_to=path) class Group(models.Model): members = models.ManyToManyField(User, related_name=’group_list’, null=True, blank=True) I have tried: var = mygroup.work_list.get(file=request.FILES[‘file’]) With the same file: Work matching query does not exist …

Total answers: 2

Comparing two lists in Python

Comparing two lists in Python Question: So to give a rough example without any code written for it yet, I’m curious on how I would be able to figure out what both lists have in common. Example: listA = [‘a’, ‘b’, ‘c’] listB = [‘a’, ‘h’, ‘c’] I’d like to be able to return: [‘a’, …

Total answers: 5

Best way to get query string from a URL in python?

Best way to get query string from a URL in python? Question: I need to get the query string from this URL https://stackoverflow.com/questions/ask?next=1&value=3 and I don’t want to use request.META. I have figured out that there are two more ways to get the query string: Using urlparse urlparse.urlparse(url).query Using url encode Use urlencode and pass …

Total answers: 4

How to find common elements in list of lists?

How to find common elements in list of lists? Question: I’m trying to figure out how to compare an n number of lists to find the common elements. For example: p=[ [1,2,3], [1,9,9], .. .. [1,2,4] >> print common(p) >> [1] Now if I know the number of elements I can do comparions like: for …

Total answers: 7

Check if two unordered lists are equal

Check if two unordered lists are equal Question: I’m looking for an easy (and quick) way to determine if two unordered lists contain the same elements: For example: [‘one’, ‘two’, ‘three’] == [‘one’, ‘two’, ‘three’] : true [‘one’, ‘two’, ‘three’] == [‘one’, ‘three’, ‘two’] : true [‘one’, ‘two’, ‘three’] == [‘one’, ‘two’, ‘three’, ‘three’] : …

Total answers: 8

Value for epsilon in Python

Value for epsilon in Python Question: Is there a standard value for (or method for obtaining) epsilon in Python? I need to compare floating point values and want to compare against the smallest possible difference. In C++ there’s a function provided numeric_limits::epsilon( ) which gives the epsilon value for any given data type. Is there …

Total answers: 5

Comparing None with built-in types using arithmetic operators?

Comparing None with built-in types using arithmetic operators? Question: Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. >>> None > 0 False >>> None == 0 False >>> None < 0 True Is comparing None using arithmetic operators well defined …

Total answers: 2

How to efficiently compare two unordered lists (not sets)?

How to efficiently compare two unordered lists (not sets)? Question: a = [1, 2, 3, 1, 2, 3] b = [3, 2, 1, 3, 2, 1] a & b should be considered equal, because they have exactly the same elements, only in different order. The thing is, my actual lists will consist of objects (my …

Total answers: 12

Python: if not val, vs if val is None

Python: if not val, vs if val is None Question: I’ve always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems: It’s not completely readable; if value is None is surely more understandable. This can …

Total answers: 5