简体   繁体   中英

Colon Operator with Deques (in Python)

I was hoping to use the colon operator with my deque but it didn't seem to work the same as a list.

I was trying something like:

myDeque = deque([0,1,2,3,4,5])
myDequeFunction(myDeque[3:])

This is the error I recieved: "TypeError: sequence index must be integer, not 'slice'"

What is the best way to do array slicing with deques?

Iterating is probably faster than brute-force methods (note: unproven) due to the nature of a deque.

>>> myDeque = collections.deque([0,1,2,3,4,5])
>>> list(itertools.islice(myDeque, 3, sys.maxint))
[3, 4, 5]

deque对象不支持切片本身,但是您可以创建一个新的双端队列:

sliced_deque = deque(list(old_deque)[3:])

collections.deque objects don't support slicing. It'd be more straightforward to make a new one.

n_deque = deque(list(d)[3:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM