473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

s[i:j:t] = t stipulation

s[i:j:t] = t (1) t must have the same length as the slice it is
replacing.

Why?
>>def foo():
.... while True:
.... yield 'a'
....
>>foo()
x = range(10)
x[::2] = foo()
This is infinite loop due to Python building a sequence out of
the iterator to check its length.

I think it might be more useful for

x[::2] = foo()

to result in an x of

['a', 1, 'a', 3, 'a', 5, 'a', 7, 'a', 9]

In other words, take (j-i)//k elements from t for abs(k) != 1.

A problem, though, arises when t is too short--the sequence
could be corrupted before an exception is thrown if you omit the
length check.

So you'd also have to define

x[::2] = 'aaa'

as resulting in

['a', 1, 'a', 2, 'a', 3, 5, 7, 9]

But perhaps that's just adding more useless complexity to the
already complex slicing rules (kudos for 'slice.indices', though
curses that the method isn't cross-referenced in more places).

--
Neil Cerutti
Nov 20 '07 #1
3 1155

"Neil Cerutti" <ho*****@yahoo.comwrote in message
news:sl********************@FIAD06.norwich.edu...
| s[i:j:t] = t (1) t must have the same length as the slice it is
replacing.

This is essentially the same rule as requiring a proper length of t for

a,b,c = t # for whatever number of targets

And people have made similar suggestions as below for that case also.

| Why?

A mismatch could be intentional or accidental. In most cases of this sort,
Python assumes 'accident', especially when intent can easily be indicated
otherwise.

| >>def foo():
| ... while True:
| ... yield 'a'
| ...
| >>foo()
| >>x = range(10)
| >>x[::2] = foo()
|
| This is infinite loop due to Python building a sequence out of
| the iterator to check its length.
|
| I think it might be more useful for
|
| x[::2] = foo()
|
| to result in an x of
|
| ['a', 1, 'a', 3, 'a', 5, 'a', 7, 'a', 9]
|
| In other words, take (j-i)//k elements from t for abs(k) != 1.

Use the appropriate itertools function to take the proper number of
elements.

| A problem, though, arises when t is too short--the sequence
| could be corrupted before an exception is thrown if you omit the
| length check.
|
| So you'd also have to define
|
| x[::2] = 'aaa'
|
| as resulting in
|
| ['a', 1, 'a', 2, 'a', 3, 5, 7, 9]

No, it should be defined as resulting in

['a', 1, 'a', 2, 'a', 3, None, 5, None, 7, None, 9] # ;-)

Or better yet, require the programmer to specify by modifying either the
target or source spec, as is done now.

Terry Jan Reedy

Nov 20 '07 #2
On 2007-11-20, Terry Reedy <tj*****@udel.eduwrote:
"Neil Cerutti" <ho*****@yahoo.comwrote in message
news:sl********************@FIAD06.norwich.edu...
| s[i:j:t] = t (1) t must have the same length as the slice it is
replacing.

This is essentially the same rule as requiring a proper length
of t for

a,b,c = t # for whatever number of targets

And people have made similar suggestions as below for that case
also.

| Why?

A mismatch could be intentional or accidental. In most cases
of this sort, Python assumes 'accident', especially when intent
can easily be indicated otherwise.
Thanks. Assignment to slices are a convenient way to insert,
assign, and delete elements, but extended slices are only good
for assignment. Perhaps I was searching for consistency in the
wrong place, though.
>| >>def foo():
| ... while True:
| ... yield 'a'
| ...
| >>foo()
| >>x = range(10)
| >>x[::2] = foo()
|
| This is infinite loop due to Python building a sequence out of
| the iterator to check its length.
|
| I think it might be more useful for
|
| x[::2] = foo()
|
| to result in an x of
|
| ['a', 1, 'a', 3, 'a', 5, 'a', 7, 'a', 9]
|
| In other words, take (j-i)//k elements from t for abs(k) != 1.

Use the appropriate itertools function to take the proper
number of elements.
And anyway my math was quite wrong. :(
>| A problem, though, arises when t is too short--the sequence
| could be corrupted before an exception is thrown if you omit the
| length check.
|
| So you'd also have to define
|
| x[::2] = 'aaa'
|
| as resulting in
|
| ['a', 1, 'a', 2, 'a', 3, 5, 7, 9]

No, it should be defined as resulting in

['a', 1, 'a', 2, 'a', 3, None, 5, None, 7, None, 9] # ;-)
I thought deletion of elements would be more similar to slice
assignment, e.g.:

x[5:] = []
--[0, 1, 2, 3, 4]
-/ /-[0, 1, 2, 3, 4, None, None, None, None, None]
Or better yet, require the programmer to specify by modifying
either the target or source spec, as is done now.
It seems a shame to accept iterators but to build a sequence out
of them, if it can be avoided. But if there's too much confusion
about what it should mean, I guess that kills the idea.

--
Neil Cerutti
Nov 21 '07 #3

"Neil Cerutti" <ho*****@yahoo.comwrote in message
news:sl*******************@FIAD06.norwich.edu...
| On 2007-11-20, Terry Reedy <tj*****@udel.eduwrote:
| No, it should be defined as resulting in
| >
| ['a', 1, 'a', 2, 'a', 3, None, 5, None, 7, None, 9] # ;-)

Note smiley.

| I thought deletion of elements would be more similar to slice
| assignment, e.g.:
|
| x[5:] = []
| --[0, 1, 2, 3, 4]
| -/ /-[0, 1, 2, 3, 4, None, None, None, None, None]
|
| Or better yet, require the programmer to specify by modifying
| either the target or source spec, as is done now.
|
| It seems a shame to accept iterators but to build a sequence out
| of them, if it can be avoided. But if there's too much confusion
| about what it should mean, I guess that kills the idea.

For the core, we agree. For your personal use, completing
def repdel(museq, slise, source):
"Replace or delete museq[slise] with items from source"
...
with the behavior you want should not be too difficult.

tjr

Nov 21 '07 #4

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

Similar topics

3
by: Jared | last post by:
Hello people, I've had major difficulties finding the right Java technology that can be used to make an e-mail applet program. I'm trying to build an applet that would be similar, but much...
14
by: Xah Lee | last post by:
Just bumped into another irresponsibility in perl. the crime in question this time is the module File::Basename. Reproduction: 1. create a directory containing a file of this name:...
44
by: flyingfred0 | last post by:
A small software team (developers, leads and even the manager when he's had time) has been using (wx)Python/PostgreSQL for over 2 years and developed a successful 1.0 release of a client/server...
3
by: Markus | last post by:
hi, i am new to the xsl-fo-pdf-stuff... i want to create a pdf document with apache-fop. in this document i do have a border around two blocks. my designer want to have round corners for the...
14
by: JKop | last post by:
Here's two functions, a "const version" and a "non-const version": char* GetFirstT(char* p) { for ( ; *p; ++p) { if ( *p == 't' ) return p; }
55
by: Robotnik | last post by:
Hello All, I want to know if we could know the size of a structyure without the use of sizeof(). Any hints.
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
34
by: Smithers | last post by:
I have some logic that populates UI controls with values from App.config. A couple of checkboxes get checked or unchecked; and items loaded into a checked list box. Two reasonable places to put...
3
by: cpanthro | last post by:
Hello, I'm using Access on XP and I am trying to count the number of cases in a Table via a query using two criteria. The criteria are being drawn from two different tables. I keep getting an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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 project—planning, coding, testing,...

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.