【发布时间】:2017-10-21 04:45:36
【问题描述】:
例如,如果我输入一个带有列表和索引的函数,我该如何对其进行编码,以便它返回一个切成两半并使用循环交换位置的列表
function([0,1,2,3,4], 2)
返回
[3, 4, 2, 0, 1]
我试过了:
def function(list_value):
first_half = list_value[:index]
second_half = list_value[(index+1):]
swapped_sequence = second_half + first_half
swapped_sequence = swapped_sequence.insert(index, index)
return swapped_sequence
【问题讨论】:
-
为什么2不倒过来?
-
到目前为止必须尝试什么?请发布您的代码。
-
first_half = list_value[:index] second_half = list_value[(index+1):] swapped_sequence = second_half + first_half swapped_sequence = swapped_sequence.insert(index, index) return swapped_sequence
-
@DaquarisGeorgino 这似乎是对所提问题的合理解决方案请更新当前和预期的输出?
-
你的输出列表没有减半。