473,395 Members | 1,692 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,395 software developers and data experts.

deque slicing

Anyone know why deques don't support slicing?
from collections import deque
d = deque(i**2 - i + 1 for i in range(10))
d deque([1, 1, 3, 7, 13, 21, 31, 43, 57, 73]) d[:5] Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence index must be integer [d[i] for i in range(5)]

[1, 1, 3, 7, 13]

Is there a performance problem with supporting slicing perhaps?

Steve
--
You can wordify anything if you just verb it.
- Bucky Katt, Get Fuzzy
Jul 18 '05 #1
2 4014
> Anyone know why deques don't support slicing?

In part, it is because of the vague notion that slicing is not what deques are
all about. While it's easy to add a getslice operation, setslice and delslice
require rearranging data outside the slice range. To me, that is a strong
indicator that an application needs a list instead of a deque.

Even getitem/setitem were a stretch (they perform best at endpoints) and were
added to make deques substitutable for lists in stack/queue applications using
d[0] and d[-1] to implement peekleft() and peekright() operations.

Deques emulate some but not all of the list API. The parts that match were done
to make deques easier to learn and to make them more substitutable for lists in
stack/queue apps. The rest of the API was excluded because the list structure
was more appropriate for those operations.
Raymond
Jul 18 '05 #2
Raymond Hettinger <vze4rx4y <at> verizon.net> writes:
Even getitem/setitem were a stretch (they perform best at endpoints) and were
added to make deques substitutable for lists in stack/queue applications
using d[0] and d[-1] to implement peekleft() and peekright() operations.


Ahh, ok, so the functionality I was really looking for is peekleft(n). That
is, a peek function that would return the first n items, starting at the
left. (If you're curious why I wanted this, see the peekable class in
http://aspn.activestate.com/ASPN/Coo.../Recipe/304373.) I assumed
that the best way to get this was slicing, but are there plans for such a
peekleft function?

Steve

Jul 18 '05 #3

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

Similar topics

16
by: newsock | last post by:
What differences between queue, deque and priority_queue? And under what situations choose one of them to use? Thanks for your help!
7
by: Jenny | last post by:
Hi, I have a class foo which will construct some objects in my code. some of the objects store int values into the data deque, while others store float values to the deque. template <class...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
5
by: yancheng.cheok | last post by:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp, i realize that deque has its own speed advantage over vector in all the aspect (except for memory deallocation). does it...
1
by: Homeworkboy | last post by:
Can anyone help me with this program? Look at the bottom of this program for help methods: /*1. Make a program that uses numbers from 1 to 100 including each ends which puts the even...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
by: subramanian100in | last post by:
In ISO/IEC 14882:2003 document, in the section '23.2.1.3 deque modifiers', the following is mentioned: iterator insert(iterator position, const T& x); void insert(iterator position, size_type...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.