我编写了以下代码段:
origilist = [6, 252, 13, 10]
decilist = list(xrange(256))
def yielding(decilist, origilist):
half_origi = (len(origilist)/2)
for n in decilist:
yield origilist[:half_origi] + n + origilist[half_origi:]
for item in yielding(decilist, origilist):
print item
当我运行代码时,我得到了:
yield origilist[:half_origi] + n + origilist[half_origi:]
TypeError: can only concatenate list (not "int") to list
是否有将整数加入另一个列表的特定索引?
感谢您的回答
答案 0 :(得分:0)
您只能连续列表,n
是列表中的项目,而不是列表。如果你想用列表连接它,你需要使用[n],这基本上是创建一个项目的列表。
通常,您也可以将+
用于字符串,但要连接的每个项目都需要为字符串。因此,如果您有两个列表并想要向其添加任何项目,则需要将该项目转换为列表。