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 1 3800
[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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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):
|
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...
|
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
|
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):
| |
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,
|
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
|
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()
|
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)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
| |
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...
| |