Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python列表中移动项目?_Python_List_Indexing_Append - Fatal编程技术网

在python列表中移动项目?

在python列表中移动项目?,python,list,indexing,append,Python,List,Indexing,Append,这是最终产品。如果其他人有什么建议,请告诉我!非常感谢你的帮助 def triple_cut(deck): ''' (list of int) -> NoneType Modify deck by finding the first joker and putting all the cards above it to the bottom of deck, and all the cards below the second joker to the top

这是最终产品。如果其他人有什么建议,请告诉我!非常感谢你的帮助

def triple_cut(deck):
    ''' (list of int) -> NoneType

    Modify deck by finding the first joker and putting all the cards above it
    to the bottom of deck, and all the cards below the second joker to the top
    of deck. 
    >>> deck = [2, 7, 3, 27, 11, 23, 28, 1, 6, 9, 13, 4]
    >>> triple_cut(deck) 
    >>> deck 
    [1, 6, 9, 13, 4, 27, 11, 23, 28, 2, 7, 3]
    '''     
    joker1 = deck.index(JOKER1)
    joker2 = deck.index(JOKER2)
    first = min(joker1, joker2)
    first_cards = []

    for cards in range(len(deck[:first])):
        cards = 0 
        pop = deck.pop(cards)
        first_cards.append(pop)

    joker1 = deck.index(JOKER1)
    joker2 = deck.index(JOKER2)
    second = max(joker1, joker2)    
    second_cards = []

    for cards in deck[second + 1:]:
        pop = deck.pop(deck.index(cards))
        second_cards.append(pop)

    second_cards.reverse()

    for card in second_cards: 
        deck.insert(0, card)

    deck.extend(first_cards)   
raah我需要输入更多,因为我的帖子大部分是代码:请添加更多详细信息sss ss

提示:

p = list('abcdefghijkl')
pivot = p.index('g')
q = p[pivot:] + p[:pivot]

列表切片是你的朋友。

第二次修订的答案

当函数完成时,它不会改变组

问题是你没有给出完整的代码。我猜是这样的:

def triple_cut(deck)
    …
    deck = q
根据你的文档字符串,你把它叫做

deck = […]
triple_cut(deck)
并且已经困惑于赋值
deck=q
没有从
triple_cut()
传播出去。您无法修改方法的形式参数,因此赋值仍为triple_cut的局部赋值,并且不会影响模块级变量
deck

写这篇文章的正确方法是

def triple_cut(cuttable)
    …
    return cuttable[first:] + cuttable[:first]

deck = […]
deck = triple_cut(deck)

为了便于解释,我将参数的名称更改为
cuttable
。您可以将参数名称保留为
deck
,但我想说明的是,当您认为要分配给deck时,实际上是在分配给cutable,并且该分配不会执行triple_cut().

这将是学习TDD的一个很好的例子。顺便说一句,你可以一步一步地开发你的代码,然后再做你的性能测试。我敢打赌你可以用独角兽的钱做得很好。好的,到目前为止我已经做了,但是现在我的问题是,在传递函数之后,没有对deck进行变异:joker1=deck.index(joker1)joker2=deck.index(joker2)如果小丑1