473,520 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specify start and length, beside start and end, in slices

Hello,
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).

Wouldn't it be nice if the Python grammar had supported this frequent
use? My idea is that the expression above might be expressed as
l[12345:>10].

This change, as far as I can see, is quite small: it affects only the
grammar and byte-compiling, and has no side effects.

The only change in syntax is that short_slice would be changed from
[lower_bound] ":" [upper_bound]
to
([lower_bound] ":" [upper_bound]) | ([lower_bound] ":>" [slice_length])

Just to show what will happen to the byte code: l[12345:12345+10] is
compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
LOAD_CONST 1 (12345)
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

I suggest that l[12345:>10] would be compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
DUP_TOP
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

Well, what do you think? I would like to hear your comments.

Have a good day (or night),
Noam Raphael
Jul 18 '05 #1
17 1688
On 2004-05-21, Noam Raphael <no***@correctme.users.sourcephorge.net> wrote:
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
[...]
Wouldn't it be nice if the Python grammar had supported this frequent
use? My idea is that the expression above might be expressed as
l[12345:>10].


It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

--
Grant Edwards grante Yow! We just joined the
at civil hair patrol!
visi.com
Jul 18 '05 #2
Grant Edwards wrote:
On 2004-05-21, Noam Raphael <no***@correctme.users.sourcephorge.net> wrote:

Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].

[...]

Wouldn't it be nice if the Python grammar had supported this frequent
use? My idea is that the expression above might be expressed as
l[12345:>10].

It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

That is true, but if the list is long, it's *much* less efficient.

Thanks for your comment,
Noam
Jul 18 '05 #3
Noam Raphael wrote:
Grant Edwards wrote:
It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

That is true, but if the list is long, it's *much* less efficient.


Considering that the interpreter special-cases some integer math
including the BINARY_ADD, it likely wouldn't take a very long list
to pass the point where they're the same.

I like the idea of the optimization, in a sense, but I don't
like the syntax and doubt that there is much performance gain to be
had. There are probably better places for people to hack on the
interpreter, and which don't need syntax changes.

-Peter
Jul 18 '05 #4
Peter Hansen wrote:
Noam Raphael wrote:
Grant Edwards wrote:
It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

That is true, but if the list is long, it's *much* less efficient.

Considering that the interpreter special-cases some integer math
including the BINARY_ADD, it likely wouldn't take a very long list
to pass the point where they're the same.


I don't understand: If the list is of length 1000000, wouldn't Grant
Edwards' suggestion make 1000000-12345 new references, and then take
only the first ten of them?
Jul 18 '05 #5
On 2004-05-21, Noam Raphael <no***@correctme.users.sourcephorge.net> wrote:
It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

That is true, but if the list is long, it's *much* less efficient.
Considering that the interpreter special-cases some integer math
including the BINARY_ADD, it likely wouldn't take a very long list
to pass the point where they're the same.


I'm afraid I don't understand either. Where do integer math
shortcuts enter the picture? It seems to me it's all about
building a (possibly long new list) which you're going to throw
away after you build another list from the front it.

Unless the compiler is smart enough to figure out what you're
aiming at and skip the intermediate list entirely.
I don't understand: If the list is of length 1000000, wouldn't
Grant Edwards' suggestion make 1000000-12345 new references,
and then take only the first ten of them?


Yes, according to my understanding of how things work, that's
what happens (my spelling is pretty inefficient for pulling
small chunks from the beginnings of long lists), so if you do
a lot of that, it may be worth worrying about.

--
Grant Edwards grante Yow! Civilization is
at fun! Anyway, it keeps
visi.com me busy!!
Jul 18 '05 #6
Noam Raphael wrote:
Peter Hansen wrote:
Noam Raphael wrote:
Grant Edwards wrote:

It's a bit less efficient, but you can currently spell that as

l[12345:][:10]

That is true, but if the list is long, it's *much* less efficient.


Considering that the interpreter special-cases some integer math
including the BINARY_ADD, it likely wouldn't take a very long list
to pass the point where they're the same.


I don't understand: If the list is of length 1000000, wouldn't Grant
Edwards' suggestion make 1000000-12345 new references, and then take
only the first ten of them?


Sorry, it was perhaps unclear that I was agreeing with you. For
an extremely short list, it's possible that it would be faster
to do Grant's method, but what I was trying to say is that even
if that's true, I expect that for a list of more than a few dozen
elements it would not be faster. Looking at it again, I suspect
that it would actually never be faster, given that probably
about as many bytecode instructions are executed, and then there's
the extra memory allocation for the temporary list, the copying,
etc.

-Peter
Jul 18 '05 #7
Peter Hansen wrote:
For an extremely short list, it's possible that it would be faster
to do Grant's method, but what I was trying to say is that even
if that's true, I expect that for a list of more than a few dozen
elements it would not be faster. Looking at it again, I suspect
that it would actually never be faster, given that probably
about as many bytecode instructions are executed, and then there's
the extra memory allocation for the temporary list, the copying,


timeit confirms this with variations on this:

c:\>python -c "import timeit as t; t = t.Timer('x[y:][:10]', 'y=10000;
x=range(y)'); print t.timeit()"

and this:

c:\>python -c "import timeit as t; t = t.Timer('x[y:y+10]', 'y=10000;
x=range(y)'); print t.timeit()"

-Peter
Jul 18 '05 #8

"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).
With an expression, I'd go for the temp var.
Wouldn't it be nice if the Python grammar had supported this frequent
use?
I take this as 'directly support' versus the current indirect support via
start+len.
My answer: superficially (in isolation) yes, but overall, in the context of
Python's somewhat minimalistic grammar/syntax, no. Two ways to slice might
easily be seen as one too many. In addition, the rationale for this, your
favorite little addition, would admit perhaps 50 others like it.
My idea is that the expression above might be expressed as l[12345:>10].
Sorry, this strike me as ugly, too much like and easily confused with
l[12345:-10], and too much looking like a syntax error.

Given that some other languages slice with (start,len) arguments (but not
then, that I remember or know of, also with a start,stop option), I am
*sure* that Guido thought carefully about the issue. A plus with his
choice is ability to offset (index) from the end *without* calling the len
function.
This change, as far as I can see, is quite small: it affects only the
grammar and byte-compiling, and has no side effects.
Except the cognitive dissonance of two *almost* identical syntaxes and the
flood of other 'small', 'no side effect' change requests.
Well, what do you think? I would like to hear your comments.


Your wish ...

Terry J. Reedy


Jul 18 '05 #9
I think it is odd that I have never encounter
many of these types of constructs repeatedly in
my code. Perhaps you could share a little more
of where you see this type of think popping up
a lot? I suspect that there is another method
for solving the problem that might be faster
and easier to read/program.

Larry Bates,
Syscon, Inc.
"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...
Hello,
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).

Wouldn't it be nice if the Python grammar had supported this frequent
use? My idea is that the expression above might be expressed as
l[12345:>10].

This change, as far as I can see, is quite small: it affects only the
grammar and byte-compiling, and has no side effects.

The only change in syntax is that short_slice would be changed from
[lower_bound] ":" [upper_bound]
to
([lower_bound] ":" [upper_bound]) | ([lower_bound] ":>" [slice_length])

Just to show what will happen to the byte code: l[12345:12345+10] is
compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
LOAD_CONST 1 (12345)
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

I suggest that l[12345:>10] would be compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
DUP_TOP
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

Well, what do you think? I would like to hear your comments.

Have a good day (or night),
Noam Raphael

Jul 18 '05 #10
Hello,

Terry Reedy wrote:
"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...

Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).

With an expression, I'd go for the temp var.

Wouldn't it be nice if the Python grammar had supported this frequent
use?

I take this as 'directly support' versus the current indirect support via
start+len.
My answer: superficially (in isolation) yes, but overall, in the context of
Python's somewhat minimalistic grammar/syntax, no. Two ways to slice might
easily be seen as one too many.


I agree that Python should be kept easy to read and understand. However,
it doesn't mean that there's only one way to do everything. An example
(it's even from slices): the Numeric people asked for the "..." token
and got it, even though you can live without it - it simply makes your
life easier.
In addition, the rationale for this, your
favorite little addition, would admit perhaps 50 others like it.

My idea is that the expression above might be expressed as l[12345:>10].

Sorry, this strike me as ugly, too much like and easily confused with
l[12345:-10], and too much looking like a syntax error.

Well, of course, it *is* a syntax error right now. As for what it looks
like - I can't argue with what it looks like to you, but since '>' is
generally perceived as having something to do with "go in the right
direction", I think that l[12345:>10] can easily be read as "start from
12345, and take 10 steps to the right. Take all the items you passed over."
Given that some other languages slice with (start,len) arguments (but not
then, that I remember or know of, also with a start,stop option), I am
*sure* that Guido thought carefully about the issue. A plus with his
choice is ability to offset (index) from the end *without* calling the len
function.
I think that the fact that other languages use (start, len) quite
contradicts your assumption that only 50 other people would like it. I
don't see what brings you to think that you represent 99.99 percent of
Python users.
I like Python's slicing very much, and I agree that given only one
slicing method, (start, end) should be chosen, but what's wrong with
adding another?
This change, as far as I can see, is quite small: it affects only the
grammar and byte-compiling, and has no side effects.

Except the cognitive dissonance of two *almost* identical syntaxes and the
flood of other 'small', 'no side effect' change requests.

Why not judge each 'small, no side effect' change request for its own
sake? Do you think that Python should only undergo big and complex changes?
Well, what do you think? I would like to hear your comments.

Your wish ...

Yes, I do like to hear other opinions. Perhaps *you* could have been a
bit more open to hear them...
Terry J. Reedy

Noam Raphael
Jul 18 '05 #11
Hello,

Larry Bates wrote:
I think it is odd that I have never encounter
many of these types of constructs repeatedly in
my code. Perhaps you could share a little more
of where you see this type of think popping up
a lot? I suspect that there is another method
for solving the problem that might be faster
and easier to read/program.

Larry Bates,
Syscon, Inc.


With pleasure. Here are two examples:

1. Say I have a list with the number of panda bears hunted in each
month, starting from 1900. Now I want to know how many panda bears were
hunted in year y. Currently, I have to write something like this:
sum(huntedPandas[(y-1900)*12:(y-1900)*12+12])
If my suggestion is accepted, I would be able to write:
sum(huntedPandas[(y-1900)*12:>12])

(Yes, I know that it may also be expressed as
sum(huntedPandas[(y-1900)*12:(y-1901)*12]), but it's less clear what I
mean, and it's still longer)

2. Many data files contain fields of fixed length. Just an example: say
I want to get the color of the first pixel of a 24-bit color BMP file.
Say I have a function which gets a 4-byte string and converts it into a
32-bit integer. The four bytes, from byte no. 10, are the size of the
header, in bytes. Right now, if I don't want to use temporary variables,
I have to write:
picture[s2i(picture[10:14]):s2i(picture[10:14])+4]
I think this is nicer (and quicker):
picture[s2i(picture[10:>4]):>4]

Thanks for your interest,
Noam Raphael
Jul 18 '05 #12

"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...
contradicts your assumption that only 50 other people would like it. I
don't see what brings you to think that you represent 99.99 percent of
Python users.
Projecting thoughts into my brain that I never had is stupid. I really
don't like that.
Perhaps *you* could have been a bit more open to hear them...


Making false ad hominen comments is stupid. I don't like that either.

I an disappointed. Sorry I took your request for comments *on the
proposal* seriously.

Terry J. Reedy


Jul 18 '05 #13
Op 2004-05-21, Terry Reedy schreef <tj*****@udel.edu>:

"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).


With an expression, I'd go for the temp var.
Wouldn't it be nice if the Python grammar had supported this frequent
use?


I take this as 'directly support' versus the current indirect support via
start+len.
My answer: superficially (in isolation) yes, but overall, in the context of
Python's somewhat minimalistic grammar/syntax, no. Two ways to slice might
easily be seen as one too many. In addition, the rationale for this, your
favorite little addition, would admit perhaps 50 others like it.
My idea is that the expression above might be expressed as l[12345:>10].


Sorry, this strike me as ugly, too much like and easily confused with
l[12345:-10], and too much looking like a syntax error.

Given that some other languages slice with (start,len) arguments (but not
then, that I remember or know of, also with a start,stop option), I am
*sure* that Guido thought carefully about the issue. A plus with his
choice is ability to offset (index) from the end *without* calling the len
function.


Well I hate his choice. It is inconsistent with the fact that generally
l[a:b] produces the empty list when a > b.

It is only inconsistent with the Zen of python which says there should
be only way to do something.

--
Antoon Pardon
Jul 18 '05 #14
Noam Raphael wrote:
I have to write:
picture[s2i(picture[10:14]):s2i(picture[10:14])+4]
I think this is nicer (and quicker):
picture[s2i(picture[10:>4]):>4]


that's spelled

picture = Image.open(file)
picture.getpixel((0, 0))

</F>


Jul 18 '05 #15
Noam Raphael <no***@correctme.users.sourcephorge.net> wrote in message news:<c8**********@news.iucc.ac.il>...
Hello,
Many times I find myself asking for a slice of a specific length, and
writing something like l[12345:12345+10].
This happens both in interactive use and when writing Python programs,
where I have to write an expression twice (or use a temporary variable).

Wouldn't it be nice if the Python grammar had supported this frequent
use? My idea is that the expression above might be expressed as
l[12345:>10].

This change, as far as I can see, is quite small: it affects only the
grammar and byte-compiling, and has no side effects.

The only change in syntax is that short_slice would be changed from
[lower_bound] ":" [upper_bound]
to
([lower_bound] ":" [upper_bound]) | ([lower_bound] ":>" [slice_length])

Just to show what will happen to the byte code: l[12345:12345+10] is
compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
LOAD_CONST 1 (12345)
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

I suggest that l[12345:>10] would be compiled to:
LOAD_GLOBAL 0 (l)
LOAD_CONST 1 (12345)
DUP_TOP
LOAD_CONST 2 (10)
BINARY_ADD
SLICE+3

Well, what do you think? I would like to hear your comments.

Have a good day (or night),
Noam Raphael


Python has ready a workaround for nearly ervery problem.
What about the following?
# iNCREMENTALslICE
isl=lambda l,start,increment:l.__getslice__(start,start+incre ment)
l='zero one two three four five six'.split()
l ['zero', 'one', 'two', 'three', 'four', 'five', 'six'] isl(l,3,3) ['three', 'four', 'five']


Regards
Peter
Jul 18 '05 #16
Fredrik Lundh wrote:
Noam Raphael wrote:

I have to write:
picture[s2i(picture[10:14]):s2i(picture[10:14])+4]
I think this is nicer (and quicker):
picture[s2i(picture[10:>4]):>4]

that's spelled

picture = Image.open(file)
picture.getpixel((0, 0))

</F>

Hello,
Thanks for your suggestion, but I meant to give an example for the need
of those slices when handling files of a format which is not already
handled by a module someone wrote.
And what if I want to write a new module for handling images?

Noam Raphael
Jul 18 '05 #17
Terry Reedy wrote:
"Noam Raphael" <no***@correctme.users.sourcephorge.net> wrote in message
news:c8**********@news.iucc.ac.il...
contradicts your assumption that only 50 other people would like it. I
don't see what brings you to think that you represent 99.99 percent of
Python users.

Projecting thoughts into my brain that I never had is stupid. I really
don't like that.

When you assume that only 50 people would like my suggestion, you assume
that all the other 99.99 percent of Python users wouldn't like it, just
because you don't. If I am wrong - correct me.
Perhaps *you* could have been a bit more open to hear them...

Making false ad hominen comments is stupid. I don't like that either.

I an disappointed. Sorry I took your request for comments *on the
proposal* seriously.

Terry J. Reedy

As you may have noticed, I did take your comments seriously, and
referred to every one of them.
I'm sorry if my remark offended you. I will try to be more polite in my
future posts. However, I did sense a tone of impatience in your reply,
and I think you should try to eliminate it in your future posts.

Best wishes,
Noam Raphael
Jul 18 '05 #18

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

Similar topics

8
1950
by: M. Clift | last post by:
Hi All, I have a list of varying length. Would someone know the way to get the last two values for this? I can see how this is done with a list that I know the length of, but not one thats generated by user input. Thanks for any help
29
3367
by: George Sakkis | last post by:
Why does slicing a tuple returns a new tuple instead of a view of the existing one, given that tuples are immutable ? I ended up writing a custom ImmutableSequence class that does this, but I wonder why it is not implemented for tuples. George
37
8831
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here is code used for measuring:
11
1315
by: John Salerno | last post by:
Given: numbers = can someone explain to me why numbers results in ? I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this list, so what is it
27
2226
by: Chris Tomlinson | last post by:
Hi, is there any way to specify the sequence in which images load on a web page? More specifically, here is what we need to achieve: Image1 starts loading first and the browser does not continue through the HTML until Image1 has loaded COMPLETELY. When Image1 is done, Image2 BEGINS loading. When Image2 is 100% done, only then does Image...
3
2398
slightlybefuddled
by: slightlybefuddled | last post by:
(Exporting ImageReady slices as CSS rather than tables) apparently means it'll work just fine in Firefox, but do wacky stuff in IE? Can anyone help me figure out why on earth the slices are not showing up in their proper places in IE(7)? The button bar I created is perfectly set up in Firefox, but when I preview in IE, not so much. The...
31
1693
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: +---+---+---+---+---+...
0
7204
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7602
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7560
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5742
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3282
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
506
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.