Test if two lists are equal [duplicate] - python

This question already has answers here:
Check if two unordered lists are equal [duplicate]
(8 answers)
Closed 7 years ago.
I'm trying to write tests for my Django app and I need to check many times
if 2 lists have the same objects (i.e. that every object in A is also in B and vice versa).
I read on the assertLists/Sequence/Equal etc but for what I saw if the lists
have the same objects but in a different order (A = [a,b,c], B = [b,c,a]) then it returns an error, which I don't want it to be an error because they both have the same objects.
Is there a way to check this without looping the lists?

You can use assertCountEqual in Python 3, or assertItemsEqual in Python 2.
From the Python 3 docs for assertCountEqual:
Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.
Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.

If you are staying with list() datatype then the cleanest way if your lists are not too large would be :
sorted(list_1) == sorted(list_2)
Have a look at this Question (which is the same):
Check if two unordered lists are equal

Related

why is the type of a tuple a list when only one list is present inside the tuple? [duplicate]

This question already has answers here:
How to create a "singleton" tuple with only one element
(4 answers)
Closed 6 years ago.
x=([1,2,3])
type(x)= List
x=([1,2,3],[4,5,6])
type(x)=tuple
why does the type change?
The proper syntax for creating a tuple with only one item is to follow the item with a comma:
x=([1,2,3],)
which for this example will in fact give
type(x)=tuple
Reference the official Python 2 documentation
which states (quote)
A special problem is the construction of tuples containing 0 or 1
items: the syntax has some extra quirks to accommodate these. Empty
tuples are constructed by an empty pair of parentheses; a tuple with
one item is constructed by following a value with a comma (it is not
sufficient to enclose a single value in parentheses).

Why does max() return an empty list as the largest item (larger than integers) [duplicate]

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 4 years ago.
I have discovered that a list is greater than a number.
>>> [1,2,3] > 1000
True
Is there some reason why this works? I can't convert a list to an int with int([1,2,3]). The int can't be converted to a list with list(1000). So how is python comparing the two?
In this case of "mismatched" types, the types are listed lexicographically by type name: a "list" comes after an "int" in alphabetical ordering, so it is greater.
CPython implementation detail: Objects of different types except
numbers are ordered by their type names; objects of the same types
that don’t support proper comparison are ordered by their address.
(source)
There is no language specification for the ordering (apart from the fact that it is consistent). It just happens to be the case that CPython is the most common implementation in which there is this language detail of being ordered lexicographically by type names.
According to the Python Reference Manual,
Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.

How does Python sort user-defined objects? [duplicate]

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 7 years ago.
What's the default order for sorting user-defined objects in Python? I know how to change this, using the key parameter or defining the __lt__ etc, methods, but not what order you get if you don't.
Python sorts objects by default in ascending order. A simple ascending sort is done by calling the sorted() function.
It returns a new sorted list.

internal comparison of string and int in python [duplicate]

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 2 years ago.
When a string is being compared to an integer are the string and int compared with the ASCII code internally, or how is it? I know that strings compare greater than integers, but how does that internal comparison takes place?
>>> "a" > 1
True
In your example, 1 < "a" because "i" for int comes alphabetically before "s" for string.
From the docs:
Objects of different types, except different numeric types and
different string types, never compare equal; such objects are ordered
consistently but arbitrarily (so that sorting a heterogeneous array
yields a consistent result).
I believe this was one of the things changed in python 3 (you would get a TypeError here).
As for how it is done in CPython, objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. Note that this is part of the implementation, not a part of the language.
You should check the source of the __gt__ method of the inbuilt string object to know the details but my guess is that the 1 is converted to a string using the str function and then then the two are compared.

Why are Python objects of different types ordered by type names? [duplicate]

This question already has answers here:
Why is '' > 0 True in Python 2?
(2 answers)
Closed 2 years ago.
From Python docs: http://docs.python.org/library/stdtypes.html#comparisons
Implementation note: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
Is there any reason for choosing to do this over raising an exception?
About four lines up from that line you quoted:
Objects of different types, except
different numeric types and different
string types, never compare equal;
such objects are ordered consistently
but arbitrarily (so that sorting a
heterogeneous array yields a
consistent result).
You don't want to raise exceptions when sorting a list of differently typed objects.
It can be useful for objects of different types to be collected into a single, sorted list, in a definite order. By giving all objects a stable sort order, this behavior is default.

Categories

Resources