Comparing two lists using the greater than or less than operator
up vote
20
down vote
favorite
I noticed a piece of code recently directly comparing two lists of integers like so:
a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
...
which seemed a bit peculiar, but I imagined it would return True
if all of list_a
's elements are larger then list_b
's and False if each element is equal or list_b
's elements are larger then list_a
's. So I tested it:
>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True
Ok that works. As does:
>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False
but when it gets more fuzzy:
>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True
or:
>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False
the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?
python list
add a comment |
up vote
20
down vote
favorite
I noticed a piece of code recently directly comparing two lists of integers like so:
a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
...
which seemed a bit peculiar, but I imagined it would return True
if all of list_a
's elements are larger then list_b
's and False if each element is equal or list_b
's elements are larger then list_a
's. So I tested it:
>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True
Ok that works. As does:
>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False
but when it gets more fuzzy:
>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True
or:
>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False
the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?
python list
add a comment |
up vote
20
down vote
favorite
up vote
20
down vote
favorite
I noticed a piece of code recently directly comparing two lists of integers like so:
a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
...
which seemed a bit peculiar, but I imagined it would return True
if all of list_a
's elements are larger then list_b
's and False if each element is equal or list_b
's elements are larger then list_a
's. So I tested it:
>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True
Ok that works. As does:
>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False
but when it gets more fuzzy:
>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True
or:
>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False
the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?
python list
I noticed a piece of code recently directly comparing two lists of integers like so:
a = [10,3,5, ...]
b = [5,4,3, ...,]
if a > b:
...
which seemed a bit peculiar, but I imagined it would return True
if all of list_a
's elements are larger then list_b
's and False if each element is equal or list_b
's elements are larger then list_a
's. So I tested it:
>>> a=[3,3,3,3]
>>> b=[4,4,4,4]
>>> a>b
False
>>> b>a
True
Ok that works. As does:
>>> b = [1,1,1,1]
>>> a = [1,1,1,1]
>>> a>b
False
>>> b>a
False
but when it gets more fuzzy:
>>> a=[1,1,3,1]
>>> b=[1,3,1,1]
>>> a>b
False
>>> b>a
True
or:
>>> a=[1,3,1,1]
>>> b=[1,1,3,3]
>>> a>b
True
>>> b>a
False
the results are a bit stranger. What is python actually doing? It seems that it's returning the result in favour of the first list in which the left most element is greater then the corresponding?
python list
python list
asked Oct 24 '12 at 15:51
Timmy O'Mahony
40k8115148
40k8115148
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
33
down vote
accepted
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.
add a comment |
up vote
3
down vote
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
.
add a comment |
up vote
0
down vote
You can use sum
for finding the sum of all integers in the list and then you can compare them.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
33
down vote
accepted
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.
add a comment |
up vote
33
down vote
accepted
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.
add a comment |
up vote
33
down vote
accepted
up vote
33
down vote
accepted
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.
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.
edited Sep 8 '17 at 15:47
aggieNick02
97711019
97711019
answered Oct 24 '12 at 15:54
gefei
13.1k33459
13.1k33459
add a comment |
add a comment |
up vote
3
down vote
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
.
add a comment |
up vote
3
down vote
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
.
add a comment |
up vote
3
down vote
up vote
3
down vote
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
.
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
.
edited Nov 17 '17 at 3:34
answered Nov 17 '17 at 3:19
user8651755
add a comment |
add a comment |
up vote
0
down vote
You can use sum
for finding the sum of all integers in the list and then you can compare them.
add a comment |
up vote
0
down vote
You can use sum
for finding the sum of all integers in the list and then you can compare them.
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use sum
for finding the sum of all integers in the list and then you can compare them.
You can use sum
for finding the sum of all integers in the list and then you can compare them.
edited Nov 11 at 18:04
grrigore
698820
698820
answered Nov 11 at 16:31
TonyPlayZ
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13052857%2fcomparing-two-lists-using-the-greater-than-or-less-than-operator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown