Python - shuffle only some elements of a list

后端 未结 8 1515
礼貌的吻别
礼貌的吻别 2020-12-06 16:52

I\'m trying to shuffle only elements of a list on 3rd till last position so the 1st two will always stay in place e.g.

list = [\'a?\',\'b\',\'c\',\'d\',\'e\'         


        
8条回答
  •  眼角桃花
    2020-12-06 17:39

    What you do is this:

    copy = list[2:]
    random.shuffle(copy)    
    

    which does not do much to the original list. Try this:

    copy = list[2:]
    random.shuffle(copy)
    list[2:] = copy # overwrite the original
    

提交回复
热议问题