473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

itertools.islic e and slice objects

Is there a reason that itertools.islic e doesn't support None arguments
for start and step? This would be handy for use with slice objects:
r = range(20)
s1 = slice(2, 10, 2)
s2 = slice(2, 10)
s3 = slice(10)
list(itertools. islice(r, s1.start, s1.stop, s1.step)) [2, 4, 6, 8] list(itertools. islice(r, s2.start, s2.stop, s2.step)) Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
TypeError: an integer is required list(itertools. islice(r, s2.start, s2.stop, s2.step or 1)) [2, 3, 4, 5, 6, 7, 8, 9] list(itertools. islice(r, s3.start, s3.stop, s3.step)) Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
TypeError: an integer is required list(itertools. islice(r, s3.start or 0, s3.stop, s3.step or 1)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Of course, I can get (start, stop, step) tuples with slice.indices, but
only if I know the length of the iterable, which kinda defeats the
purpose of using itertools...
list(itertools. islice(r, *s2.indices(10) )) [2, 3, 4, 5, 6, 7, 8, 9] list(itertools. islice(r, *s3.indices(10) ))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Steve
Jul 18 '05 #1
1 3793
[Steven Bethard]
Is there a reason that itertools.islic e doesn't support None arguments
for start and step? This would be handy for use with slice objects
Offhand, this seems like a reasonable idea. Please file a Py2.5 feature request
and assign to me.

Of course, I can get (start, stop, step) tuples with slice.indices, but
only if I know the length of the iterable, which kinda defeats the
purpose of using itertools...


The main purpose is to extract elements one at time instead of all at once.
This memory friendly approach results in better scalability.
Raymond Hettinger
Jul 18 '05 #2

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

Similar topics

1
2304
by: Dan Williams | last post by:
Is there any interest in adding a "Haskellish" take function to itertools? I know its easy enough to roll my own, I've been using: take = lambda iter, n: which works, but I was wondering if it was common enough to get into itertools? -Dan
15
2495
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index >>> b = a() >>> print b Traceback (most recent call last):
21
2188
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import itertools as it' in code. iter could also > become a type wrapper instead of a function, so an iter instance could > be a wrapper that figures out whether to call .next or __getitem__ > depending on it's argument. > for item in...
23
2343
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in tree.items('alfa.': 'beta.'): -- Antoon Pardon
7
2218
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69, Apr 11 2006, 15:32:42) on win32 Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last):
23
1472
by: Mathias Panzenboeck | last post by:
I wrote a few functions which IMHO are missing in python(s itertools). You can download them here: http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104 A short description to all the functions: icmp(iterable1, iterable2) -integer Return negative if iterable1 < iterable2, zero if iterable1 == iterable1,
3
6240
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I'm a python newbie. It seems the slice operation will do copy. for example: if the list have large members, the slice operations will consume many times. for instance, I have a long string named it as S, the size is more than 100K
4
6792
by: Matthew Wilson | last post by:
I want to write a function that each time it gets called, it returns a random choice of 1 to 5 words from a list of words. I can write this easily using for loops and random.choice(wordlist) and random.randint(1, 5). But I want to know how to do this using itertools, since I don't like manually doing stuff like: phrase = list()
3
2351
by: bearophileHUGS | last post by:
I find itertools.islice() useful, so for Python 3.x I may like to see it removed from the itertools module, and the normal slicing syntax extended to work with generators/iterators too. from itertools import islice primes = (x for x in xrange(1,999) if all(x % y for y in xrange(2, x))) print list(islice(primes, 0, 20)) ==> print list(primes)
0
9541
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
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 we have to send another system
3
2938
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.