Skip to main content
 首页 » 编程设计

python之List.extend() 在 Python 中没有按预期工作

2025年05月04日16mate10pro

<分区>

我有一个列表queue和一个迭代器对象neighbors,我想将其元素附加到列表中。

queue = [1] 
neighbor = T.neighbors(1) #neighbor is a <dict_keyiterator at 0x16843d03368> 
print(list(neighbor)) #Output: [2, 3] 
queue.extend([n for n in neighbor]) 
print(queue) 

输出:

[1] 

预期输出:

[1, 2, 3] 

出了什么问题?