From Comparing Sequences and Other Types in the Python tutorial:
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
See also the Wikipedia article about lexicographical order.
Answer from gefei on Stack OverflowFrom Comparing Sequences and Other Types in the Python tutorial:
The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.
See also the Wikipedia article about lexicographical order.
Since I didn't find the explanation of list/tuple comparison using "lexicographical ordering" particularly illuminating at first, here's an attempt to explain it "in my own words". First, here are some example lists that are referred to in the explanation below:
a = [1, 2, 3]
b = [1, 2, 10]
c = [1, 2, 3, 100]
d = [1, 2, 3]
e = [1, 2, 3, 4, 'a']
f = ['a', 'b', 'c']
The pair of items at each index are compared in turn. So, comparing a to b will result in 1 being compared to 1, 2 being compared to 2, and 3 being compared to 10.
The comparison of pairs will stop when either an unequal pair of items is found or--if the lists are different lengths--the end of the shorter list is reached.
For example, when comparing a and b, comparisons will stop when 3 and 10 are compared. When comparing b and c, comparisons will stop when 10 and 3 are compared.
As soon as an unequal pair is found, the overall result is the result of comparing the unequal items. This applies whether the lists are the same length or not--for example, list b is greater than list c because the 100 in c never comes into play.
For example, when comparing a to b, the overall result will be the result of comparing 3 to 10. a < b -> True because 3 is less than 10. a > b -> False because 3 is not greater than 10. a == b -> False because 3 does not equal 10.
If one of the lists is shorter and its N items are equal to the first N items of the longer list, as with a and c, the shorter list will be considered less than the longer list (so a is less than c).
Two lists will compare as equal only if they're the same length and all pairs of items compare as equal.
Note about types: if the items in a pair aren't comparable, the comparison will fail with a TypeError as usual. For example, comparing list a to f will fail when 1 is compared to 'a'. But also note that lists d and e can be compared since the 'a' in e is never compared to anything in d.
Hi All
I have a list and i want to compare if the each item is greater than the previous item but i'm getting stuck in my logic, this is what i have so far. Where am i going wrong please?
list2=[1,2,2,3,3,4,4,5]
for i in list2:
if i == list2[i-1]:
print(i)How to compare two lists for the greater values in Python - Stack Overflow
python - Comparing two list with greater than > or less than < - Stack Overflow
How can I compare two lists in python and return matches - Stack Overflow
Python: How do you write this following in greater than and less than form?
If you mean that list3 is the collection of values from list1 where the corresponding value in list2 is smaller, then:
list3 = [item1 for item1, item2 in zip(list1, list2) if item1 > item2]
You say "list", but from the error message and the outputs I suspect you're working with numpy arrays. In any case, the problem with v = [c for c in f if c > y] is that you're comparing an element c with the array y. You want to compare element to element.
In the case of a list, you can do the following:
>>> aa = [2,3,4,5]
>>> bb = [3,4,2,5]
>>>
>>> z = [a for a,b in zip(aa,bb) if a > b]
>>> z
[4]
Or if you're really working with numpy arrays, you can make it even easier:
>>> import numpy
>>> aa = numpy.array([2,3,4,5])
>>> bb = numpy.array([3,4,2,5])
>>> aa > bb
array([False, False, True, False], dtype=bool)
>>> aa[aa > bb]
array([4])
Not the most efficient one, but by far the most obvious way to do it is:
>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
{5}
if order is significant you can do it with list comprehensions like this:
>>> [i for i, j in zip(a, b) if i == j]
[5]
(only works for equal-sized lists, which order-significance implies).
Use set.intersection(), it's fast and readable.
>>> set(a).intersection(b)
set([5])