简体   繁体   English

为什么我的代码没有更改列表?

[英]Why doesn't my code mutate the list?

My code is the following (i don't know why it isn't indented): 我的代码如下(我不知道为什么不缩进):

def move_joker_2(deck):

#start checking every card in the deck until the position of 
#JOKER2 is found

#we start from position 0
position = 0

while deck [position] != JOKER2:
    position = position + 1

#the new position for JOKER2
if position < len (deck) - 3:
    new_position = position + 2
elif position == len (deck) - 2:
    new_position = 0
else:
    new_position = 1

#to reorganize deck, we need 2 things
# 1.) a list of the cards above JOKER2 after moving the cards
# 2.) a list of the cards below JOKER2 after moving the cards

#depending of new_position, there are 3 possible new decks

if new_position == 0:
    #no cards above, JOKER2 will become the top card
    cards_above = []
    #every other card will be below it
    cards_below = deck
    #remove JOKER2, since we moved it
    cards_below.pop(position)

elif new_position == 1:
    #the only card above JOKER2 will be the top card
    cards_above = [deck[0]]     

    #every other card up except the last one will be below it
    cards_below = deck [new_position:len(deck)-1]

else:   

    cards_above = deck[0:new_position+1]    
    #remove JOKER2, since we moved it
    cards_above.pop(position)
    cards_below = deck [new_position+1:]

#final deck
deck = cards_above + [JOKER2] + cards_below

My code receives a list of strings and mutates it in the end... 我的代码收到一个字符串列表,最后对其进行了变异。

But why doesn't it change the original list? 但是为什么它不更改原始列表? For example: 例如:

deck = [1, 3, 27, 8, 9] move_joker_2(deck) 甲板= [1、3、27、8、9] move_joker_2(甲板)

It should change the list, considering that JOKER2 is 27, to: [1, 3, 8, 9, 27] 考虑到JOKER2为27,它应该将列表更改为:[1、3、8、9、27]

But whenever I call deck it hasn't changed... 但是每当我打电话给甲板时,它都没有改变...

deck = cards_above + [JOKER2] + cards_below does not change the content of the deck . deck = cards_above + [JOKER2] + cards_below不会更改deck的内容。

It make a new list and deck reference that new list. 它使一个新的列表和deck参照新的名单。

To change the content of the desk use slice notation like deck[:] = cards_above + [JOKER2] + cards_below . 要更改桌面的内容,请使用诸如deck[:] = cards_above + [JOKER2] + cards_below类的切片符号。

>>> def f1(deck):
...     deck = [1,2,3] # This does not change the `deck` passed.
                       # This just create a local variable `deck`.
...
>>> def f2(deck):
...     deck[:] = [4,5,6] # This change the `deck` passed.
...
>>> deck = [0]
>>> f1(deck)
>>> deck
[0]
>>> f2(deck)
>>> deck
[4, 5, 6]

deck passed as an argument is just a variable with the copy of the reference so when you assign 作为参数传递的deck只是带有引用副本的变量,因此当您分配时

deck = ....

you create the new object and assign its reference to the deck variable. 您创建新对象,并将其引用分配给deck变量。 These are not references in the c++ sense, these are always copies of references. 这些不是 c ++的引用,它们始终是引用的副本。

One opssible walkaround would be to use 一种可行的解决方法是使用

deck[:] = ...

which updates the content of the object, not just the reference value 这将更新对象的内容,而不仅仅是参考值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM