首先,我已经看过这些帖子上的同一主题:类型错误:只能加入一个可迭代的Python错误
TypeError: can only join an iterable python
"Can only join an iterable" python error
"Can only iterable" Python error
python error-can only join an iterable
但可悲的是,他们似乎都没有回答我的问题。我想获取一个列表,并按大小对所有非-1值进行排序,然后将-1返回到它们的原始位置。这里是我的代码:
def sortByHeight(a):
treeLocations = []
for everyHeight in a:
if everyHeight == -1:
treeLocations.append([(len(a)-a.index(everyHeight)), (a.index(everyHeight)-1)])
a.remove(-1)
people = sorted(a)
for everyPair in treeLocations:
one = everyPair[1]
two = everyPair[0]
people[one:two] = -1
return(people)
sortByHeight([-1, 150, 190, 170, -1, -1, 160, 180])
我的代码引发错误:类型错误:只能指定一个迭代,上线11 这是什么意思,以及如何解决呢?
你需要检查你的缩进 – Harrichael
我改变了我的缩进,但仍然得到错误... – Bobawob
'people [one:two] = -1' < - 你正在尝试给列表片分配一个整数。 – zwer