You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现列表右移填充首值 调用函数无返回结果问题排查

问题排查

你的函数逻辑完全符合需求,调用后确实会生成预期的[1,1,1,2,3,4,5,6]结果,看不到返回值是因为没有对函数返回结果做输出操作:

  • 如果你是在.py脚本中运行这段代码:Python不会自动打印函数的返回值,需要主动调用print()输出结果
  • 如果你是在Python交互解释器中运行:直接写函数调用会自动打印返回值,不需要额外加print
修正后代码
def right_shift(inputList, position):
    shiftedList = []
    for i in range(position):
        shiftedList.append(inputList[0])
    for i in range(len(inputList)):
        shiftedList.append(inputList[i])
    
    return shiftedList

# 增加print输出返回结果
print(right_shift([1,2,3,4,5,6], 3))
更简洁的Python实现

可以直接用列表拼接简化代码,效果完全一致:

def right_shift(inputList, position):
    # 直接生成填充段 + 原列表的新列表
    return [inputList[0]] * position + inputList

print(right_shift([1,2,3,4,5,6], 3))

内容的提问来源于stack exchange,提问作者Daniel Barat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 03:54:04