I am trying to create a deque of strings, b开发者_运维技巧ut when I add a string to the deque it always breaks the string up into individual characters. Here is my code so far:
from collections import deque
my_string = "test"
my_queue = deque(my_string)
print my_queue
The output I get is:
deque(['t', 'e', 's', 't'])
I would like the output to be:
deque(['test'])
Any thoughts?
The deque constructor takes an iterable as a parameter, if you just give the string to it, it will interpret it as a sequence of characters.
In order to do what you want you should wrap your string into a list:
your_string = 'string'
wrap_list = [your_string]
#Now create the deque
d = deque(wrap_list)
Of course you can do everything in one step:
your_string = 'string'
d = deque([your_string])
deque([my_string])
deque takes in a list of items. A string is a list of characters, so the deque breaks the string in to characters. If you want to add whole strings at a time, you have to add the string into an array
['test1', 'test2', 'etc']
To answer your question: you need to use deque(['test'])
精彩评论