gpt4 book ai didi

python - 反向列表,列表乘以 -1

转载 作者:太空宇宙 更新时间:2023-11-04 08:19:52 24 4
gpt4 key购买 nike

预期结果:

>>> createLists(5)
([1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [ -5, -4, -3, -2, -1], [-1, -2, -3, -4, -5])

我的代码:

  # Write a function that returns 4 lists given a postive number.
def createLists(num):
ls = [item for item in range(num)]
return set(ls, ls.reverse(), ls.reverse()*(-1), ls*(-1))

有人指导我怎么做吗?

最佳答案

def createLists(num): 
ls = range(1, num+1)
return (ls, ls[::-1], [-n for n in ls][::-1], [-n for n in ls])

print createLists(5)

http://ideone.com/B0ixx

关于python - 反向列表,列表乘以 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7277177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com