【发布时间】:2016-02-16 12:53:43
【问题描述】:
我如何找到列表的最后一个数字,然后该数字将取卡的前 x 个数字并将其放在底部,例如 cards = [1, 2, 3, 4, 27, 28, 5, 6, 7, 2],由于 2 是最后一个数字,它将获得前 2 个卡和把它放在它之前的底部,这样它就会变成[3, 4, 27, 28, 5, 6, 7, 1, 2, 2])。
我也有交换功能,但不知道如何使用它,因为它需要一个列表和一个索引,并通过索引与下一个数字交换列表。
还有一种特殊情况,当[-1] 的值为 3 时,它将与 2 的值交换,但不改变数字
[1,2,3,4,5,6,1,2,3] -> [3,4,5,6,1,2,1,2,3]
def top_to_bottom(cards)
cards = cards.index(end)
def swap_me(cards, index)
if index == len(cards) - 1:
index2 = 0
else:
index2 = index + 1
cards[index], cards[index2] = cards[index2], cards[index]
【问题讨论】:
-
您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples