How can a comparison operator be used with a[i:] < b[i:]? [duplicate]
up vote
3
down vote
favorite
This question already has an answer here:
String comparison technique used by Python
8 answers
Reading some Python code, I discovered this syntax if a[i:] < b[j:]
and the colon threw me for a loop. I found this great question/answer about it:
Colon (:) in Python list index
But then I looked back at my code example, and it's still unclear how it's using what I understand to be a shortcut for splice in a comparison.
I'm attempting to reverse engineer this into a JavaScript equivalent function. That weird comparison is the only thing I can't comprehend. What exactly is python comparing? String length? or something else?
def combineStrings(a, b):
answer = ''
a += '~'
b += '~'
i = 0
j = 0
while a[i] != '~' or b[j] != '~':
print (i, a[i:], b[j:], a[i:] < b[j:])
if a[i] != '~' and a[i:] < b[j:]:
answer += a[i]
i += 1
else:
answer += b[j]
j += 1
print (answer)
combineStrings('TACO', 'CAT')
Output
0 TACO~ CAT~ False
0 TACO~ AT~ False
0 TACO~ T~ True
1 ACO~ T~ True
2 CO~ T~ True
3 O~ T~ True
4 ~ T~ False
CATACOT
python string-comparison
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
up vote
3
down vote
favorite
This question already has an answer here:
String comparison technique used by Python
8 answers
Reading some Python code, I discovered this syntax if a[i:] < b[j:]
and the colon threw me for a loop. I found this great question/answer about it:
Colon (:) in Python list index
But then I looked back at my code example, and it's still unclear how it's using what I understand to be a shortcut for splice in a comparison.
I'm attempting to reverse engineer this into a JavaScript equivalent function. That weird comparison is the only thing I can't comprehend. What exactly is python comparing? String length? or something else?
def combineStrings(a, b):
answer = ''
a += '~'
b += '~'
i = 0
j = 0
while a[i] != '~' or b[j] != '~':
print (i, a[i:], b[j:], a[i:] < b[j:])
if a[i] != '~' and a[i:] < b[j:]:
answer += a[i]
i += 1
else:
answer += b[j]
j += 1
print (answer)
combineStrings('TACO', 'CAT')
Output
0 TACO~ CAT~ False
0 TACO~ AT~ False
0 TACO~ T~ True
1 ACO~ T~ True
2 CO~ T~ True
3 O~ T~ True
4 ~ T~ False
CATACOT
python string-comparison
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
what you are really asking here?
– ddor254
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
Don't let the last colon confuse you, it belongs to theif
statement.a[i:]
andb[j:]
is just the standard notation fora
from the i-th index on until the end.
– Christian König
yesterday
3
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...T
==T
, buta
<~
...
– Christian König
yesterday
2
It is lexicographically comparing substrings.TACO~
would come afterCAT~
in a dictionary, but beforeT~
.
– Amadan
yesterday
|
show 2 more comments
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This question already has an answer here:
String comparison technique used by Python
8 answers
Reading some Python code, I discovered this syntax if a[i:] < b[j:]
and the colon threw me for a loop. I found this great question/answer about it:
Colon (:) in Python list index
But then I looked back at my code example, and it's still unclear how it's using what I understand to be a shortcut for splice in a comparison.
I'm attempting to reverse engineer this into a JavaScript equivalent function. That weird comparison is the only thing I can't comprehend. What exactly is python comparing? String length? or something else?
def combineStrings(a, b):
answer = ''
a += '~'
b += '~'
i = 0
j = 0
while a[i] != '~' or b[j] != '~':
print (i, a[i:], b[j:], a[i:] < b[j:])
if a[i] != '~' and a[i:] < b[j:]:
answer += a[i]
i += 1
else:
answer += b[j]
j += 1
print (answer)
combineStrings('TACO', 'CAT')
Output
0 TACO~ CAT~ False
0 TACO~ AT~ False
0 TACO~ T~ True
1 ACO~ T~ True
2 CO~ T~ True
3 O~ T~ True
4 ~ T~ False
CATACOT
python string-comparison
This question already has an answer here:
String comparison technique used by Python
8 answers
Reading some Python code, I discovered this syntax if a[i:] < b[j:]
and the colon threw me for a loop. I found this great question/answer about it:
Colon (:) in Python list index
But then I looked back at my code example, and it's still unclear how it's using what I understand to be a shortcut for splice in a comparison.
I'm attempting to reverse engineer this into a JavaScript equivalent function. That weird comparison is the only thing I can't comprehend. What exactly is python comparing? String length? or something else?
def combineStrings(a, b):
answer = ''
a += '~'
b += '~'
i = 0
j = 0
while a[i] != '~' or b[j] != '~':
print (i, a[i:], b[j:], a[i:] < b[j:])
if a[i] != '~' and a[i:] < b[j:]:
answer += a[i]
i += 1
else:
answer += b[j]
j += 1
print (answer)
combineStrings('TACO', 'CAT')
Output
0 TACO~ CAT~ False
0 TACO~ AT~ False
0 TACO~ T~ True
1 ACO~ T~ True
2 CO~ T~ True
3 O~ T~ True
4 ~ T~ False
CATACOT
This question already has an answer here:
String comparison technique used by Python
8 answers
python string-comparison
python string-comparison
edited yesterday
asked yesterday
AnonymousSB
51912
51912
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Chris_Rands
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
what you are really asking here?
– ddor254
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
Don't let the last colon confuse you, it belongs to theif
statement.a[i:]
andb[j:]
is just the standard notation fora
from the i-th index on until the end.
– Christian König
yesterday
3
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...T
==T
, buta
<~
...
– Christian König
yesterday
2
It is lexicographically comparing substrings.TACO~
would come afterCAT~
in a dictionary, but beforeT~
.
– Amadan
yesterday
|
show 2 more comments
what you are really asking here?
– ddor254
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
Don't let the last colon confuse you, it belongs to theif
statement.a[i:]
andb[j:]
is just the standard notation fora
from the i-th index on until the end.
– Christian König
yesterday
3
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...T
==T
, buta
<~
...
– Christian König
yesterday
2
It is lexicographically comparing substrings.TACO~
would come afterCAT~
in a dictionary, but beforeT~
.
– Amadan
yesterday
what you are really asking here?
– ddor254
yesterday
what you are really asking here?
– ddor254
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
Don't let the last colon confuse you, it belongs to the
if
statement. a[i:]
and b[j:]
is just the standard notation for a
from the i-th index on until the end.– Christian König
yesterday
Don't let the last colon confuse you, it belongs to the
if
statement. a[i:]
and b[j:]
is just the standard notation for a
from the i-th index on until the end.– Christian König
yesterday
3
3
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...
T
== T
, but a
<~
...– Christian König
yesterday
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...
T
== T
, but a
<~
...– Christian König
yesterday
2
2
It is lexicographically comparing substrings.
TACO~
would come after CAT~
in a dictionary, but before T~
.– Amadan
yesterday
It is lexicographically comparing substrings.
TACO~
would come after CAT~
in a dictionary, but before T~
.– Amadan
yesterday
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
It's comparing by Lexicographical Order
If you're trying to find the character in b
(T) that is as least as big as a
(T) and insert all the consecutive letters in a
(A, C, O) that are smaller that character in b
, this code makes sense.
~
is the biggest printable ASCII character (126), hence it's used as a comparison.
0 TACO~ AT~ False # because 'T' < 'A'
0 TACO~ T~ True # because '~' > 'A'
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
It's comparing by Lexicographical Order
If you're trying to find the character in b
(T) that is as least as big as a
(T) and insert all the consecutive letters in a
(A, C, O) that are smaller that character in b
, this code makes sense.
~
is the biggest printable ASCII character (126), hence it's used as a comparison.
0 TACO~ AT~ False # because 'T' < 'A'
0 TACO~ T~ True # because '~' > 'A'
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
add a comment |
up vote
3
down vote
accepted
It's comparing by Lexicographical Order
If you're trying to find the character in b
(T) that is as least as big as a
(T) and insert all the consecutive letters in a
(A, C, O) that are smaller that character in b
, this code makes sense.
~
is the biggest printable ASCII character (126), hence it's used as a comparison.
0 TACO~ AT~ False # because 'T' < 'A'
0 TACO~ T~ True # because '~' > 'A'
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
It's comparing by Lexicographical Order
If you're trying to find the character in b
(T) that is as least as big as a
(T) and insert all the consecutive letters in a
(A, C, O) that are smaller that character in b
, this code makes sense.
~
is the biggest printable ASCII character (126), hence it's used as a comparison.
0 TACO~ AT~ False # because 'T' < 'A'
0 TACO~ T~ True # because '~' > 'A'
It's comparing by Lexicographical Order
If you're trying to find the character in b
(T) that is as least as big as a
(T) and insert all the consecutive letters in a
(A, C, O) that are smaller that character in b
, this code makes sense.
~
is the biggest printable ASCII character (126), hence it's used as a comparison.
0 TACO~ AT~ False # because 'T' < 'A'
0 TACO~ T~ True # because '~' > 'A'
edited yesterday
AnonymousSB
51912
51912
answered yesterday
iamanigeeit
4009
4009
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
add a comment |
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
Mind blown, thank you! I've never once tried comparing strings like this, which I just learned is also supported in JavaScript.
– AnonymousSB
yesterday
add a comment |
what you are really asking here?
– ddor254
yesterday
@ddor254 I'm asking how a slice notation can be used to compare strings? does python automatically compare length? or is it comparing something else?
– AnonymousSB
yesterday
Don't let the last colon confuse you, it belongs to the
if
statement.a[i:]
andb[j:]
is just the standard notation fora
from the i-th index on until the end.– Christian König
yesterday
3
So your problem is not about string slicing, but string comparison. It is a "simple" lexicographical comparison...
T
==T
, buta
<~
...– Christian König
yesterday
2
It is lexicographically comparing substrings.
TACO~
would come afterCAT~
in a dictionary, but beforeT~
.– Amadan
yesterday