【发布时间】:2018-11-18 18:44:45
【问题描述】:
我正在尝试创建一个函数,该函数允许将一个列表中最多 x 数量的项目附加到另一个列表中。计数器将返回在达到限制之前附加到列表中的项目数量(在这种情况下限制为 10)
我现在的代码是:
x = 10
def multienqueue(queue, items):
counter = 0
while len(queue) < x:
for i in items:
queue.append(i)
counter += 1
return counter
但是,我收到的输出是:
list = [4, 5, 6, 7, 8, 9, 'cow']
Trying to enqueue the list ['a', 'b', 'c', 'd', 'e']
The number added should be 3.
The number added was 5
The queue should now be: [4, 5, 6, 7, 8, 9, 'cow', 'a', 'b', 'c']
Your queue is: [4, 5, 6, 7, 8, 9, 'cow', 'a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e'] 作为 items 参数传递,[4, 5, 6, 7, 8, 9, 'cow'] 被传递作为队列,非常感谢您对我做错的任何帮助!
【问题讨论】:
-
问题在于内部
for循环。在检查长度之前,它会附加items中的所有内容。 -
菜鸟错误!谢谢你:)
标签: python python-3.x list loops queue