473,473 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's the difference between these 2 statements?

What's the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What's the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?
Thanks,
AT
Jul 19 '05 #1
11 1298
ATSkyWalker wrote:
What's the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What's the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?


-1.

Reinhold
Jul 19 '05 #2
Reinhold Birkenfeld wrote:
ATSkyWalker wrote:
What's the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What's the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?

-1.


-len(s) or less.
-1 will return an empty string.

Actually you start from len(s)-1 (len(s) is not an index in s) and you
stop when you reach the index specified (or the end). Since -1 is the
same index as the starting one (-1~>len(s)-1, -2~>len(s)-2,
-len(s)+1~>0), you end up with an empty string.

Therefore you have to try to reach indices lower (due to the negative
step) than the minimum valid index of your list in order to reverse it
fully.
Jul 19 '05 #3
s[len(s):-1:-1] yields an empty list !

Test code :

s = "12345"
print s[len(s)::-1] -> prints "54321"
print s[len(s):-1:-1] -> prints "" (nothing)

Jul 19 '05 #4
tiissa wrote:
Reinhold Birkenfeld wrote:
ATSkyWalker wrote:
What's the difference between these 2 statements?

If you have a String s="12345"

s[len(s)::-1] = "54321"

But

s[len(s):0:-1] = "5432"

Why? What's the difference? What number then can I use as the end of
the slice if I were to supply all 3 parameters?

-1.


-len(s) or less.
-1 will return an empty string.

Actually you start from len(s)-1 (len(s) is not an index in s) and you
stop when you reach the index specified (or the end). Since -1 is the
same index as the starting one (-1~>len(s)-1, -2~>len(s)-2,
-len(s)+1~>0), you end up with an empty string.

Therefore you have to try to reach indices lower (due to the negative
step) than the minimum valid index of your list in order to reverse it
fully.


Right, sorry.

Well, I guess that's why one can leave out the index...

Reinhold
Jul 19 '05 #5
I'm sorry, I'm not really following your logic. Can you supply the
statement with the three parameters ?

so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
or is it impossible to express it in this way ?

Thanks,
AT

Jul 19 '05 #6
ah****@gmail.com wrote:
so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
or is it impossible to express it in this way ?


This does not work for integers, because the theoretically correct value
x = -1 already has another interpretation as the gap between the last and
the last but one character. Here are two workarounds:

1. Set x to None
s = "12345"
s[len(s):None:-1] '54321'

2. Separate slicing operation and reversal:
s = "12345"
s[0:len(s)][::-1]

'54321'

Peter

Jul 19 '05 #7
Peter,

I like the way you put it "the gap between the last and
the last but one character" :-).

I guess this is a side effect of of python's asymetric slice indexing
approach which takes a little getting used to.

AT

Jul 19 '05 #8
ah****@gmail.com wrote:
I'm sorry, I'm not really following your logic. Can you supply the
statement with the three parameters ?

so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
or is it impossible to express it in this way ?


Contrary to what I said above x should be _strictly_ less than -len(s).
You stop when you reach in the list the given end index (and don't take
the item there) or if you leave the index range.

But -1 as an index is the same as (len(s)-1).
Therefore going from len(s)-1 down to -1 is the same as going from
len(s)-1 to len(s)-1 hence an empty list.

And -len(s) is the same as 0 (my mistake above)
But -len(s)-1 is not in the list thus you won't discard any limit.

The example:
In [1]: s='12345'

In [2]: s[len(s)-1:0:-1]
Out[2]: '5432'

In [3]: s[len(s)-1:-1:-1]
Out[3]: ''

In [4]: s[-1],s[len(s)-1]
Out[4]: ('5', '5')

In [5]: s[len(s)-1:-len(s)-1:-1]
Out[5]: '54321'

In [6]: s[len(s)-1:-len(s):-1]
Out[6]: '5432'
Jul 19 '05 #9
Peter Otten wrote:
ah****@gmail.com wrote:

so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
or is it impossible to express it in this way ?

This does not work for integers, because the theoretically correct value
x = -1 already has another interpretation as the gap between the last and
the last but one character.

AFAIK, it is not an issue of integer (what else can an slice index be in
python?) but simply of index aliasing.

For x=-len(s)-1, you get the whole reversed list:

In [5]: s[len(s)-1:-len(s)-1:-1]
Out[5]: '54321'
Jul 19 '05 #10
tiissa wrote:
Peter Otten wrote:
ah****@gmail.com wrote:

so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
or is it impossible to express it in this way ?

This does not work for integers, because the theoretically correct value
x = -1 already has another interpretation as the gap between the last and
the last but one character.

AFAIK, it is not an issue of integer (what else can an slice index be in
python?) but simply of index aliasing.

For x=-len(s)-1, you get the whole reversed list:

In [5]: s[len(s)-1:-len(s)-1:-1]
Out[5]: '54321'


Clever. I didn't think of that.
Still, for practical purposes you have to test for slicelen >= stringlen, so
whether you choose None, -len(s)-1, or -sys.maxint as the second slice
parameter doesn't matter much.

Peter

Jul 19 '05 #11
Peter Otten wrote:
Still, for practical purposes you have to test for slicelen >= stringlen, so
whether you choose None, -len(s)-1, or -sys.maxint as the second slice
parameter doesn't matter much.


Sure, for practical purposes you don't bother to write extra characters
and leave it void.
But we knew it from the start of the thread. ;)
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: lkrubner | last post by:
Are there any benchmarks on how much an extra, unneeded VARCHAR, CHAR, INT, BIGINT, TEXT or MEDIUMTEXT slows down a database call with MySql? PostGre info would also be useful. I'm trying to...
1
by: Brittany | last post by:
can someone explain to me what if else staments do and what while statements do.
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
6
by: michaaal | last post by:
What is the difference between these two statements? They seem to do the same thing... response.write(request("variable")) response.write(request.querystring("variable"))
7
by: pauldepstein | last post by:
A book I have describes a class called vector. It then explains (and I understand) the concept of a copy constructor, which is needed to interpret statements like vector b = a; But then I...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
16
by: nephish | last post by:
Hey there, i have been learning python for the past few months, but i can seem to get what exactly a lamda is for. What would i use a lamda for that i could not or would not use a def for ? Is...
5
by: raghu | last post by:
can anyone please tell me difference between the two statements. #define ME int and typedef int ME Both statements do the same work.. right? Thanks a lot in advance.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.