慕工程0101907
使用zip我认为你想要的代码是:rows = list(zip(sortedList, reversed(sortedList)))但是,请注意,这会“重复”元素:>>> sortedList = [1, 2, 3, 4, 5]>>> list(zip(sortedList, reversed(sortedList)))[(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]如果您知道列表有偶数个元素并且想避免重复,您可以改为编写:rows = list(zip(sortedList[:len(sortedList)//2], reversed(sortedList[len(sortedList)//2:])))结果如下:>>> sortedList = [1,2,3,4,5,6]>>> list(zip(sortedList[:len(sortedList)//2], reversed(sortedList[len(sortedList)//2:])))[(1, 6), (2, 5), (3, 4)]使用循环尽管我建议使用zip而不是for-loop,但以下是修复您编写的循环的方法:rows = []for i in range(len(sortedList)): rows.append((sortedList[i], sortedList[-i-1]))结果:>>> sortedList=[1,2,3,4,5]>>> rows = []>>> for i in range(len(sortedList)):... rows.append((sortedList[i], sortedList[-i-1]))... >>> rows[(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]