Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x_List_Loops_Methods - Fatal编程技术网

Python 将特定项目移动到列表的末尾

Python 将特定项目移动到列表的末尾,python,python-3.x,list,loops,methods,Python,Python 3.x,List,Loops,Methods,我有一个列表,我想将带有“1”的元素移动到列表的末尾 例如,我有以下列表: 测试=[['dogs',2],'cats',3],'dogs',4],'dogs',1],'cats',11],'cats',1],'birds',1],'birds',12]] 我想将第二个元素为“1”的所有列表移动到列表“Test”的末尾。到目前为止, def评分手(手): 但是,执行此操作时,我的循环跳过一个元素。我是一个新的程序员,我不知道该怎么做 我不确定python中的实现细节,但在大多数语言中,在迭代时修改

我有一个列表,我想将带有“1”的元素移动到列表的末尾

例如,我有以下列表: 测试=[['dogs',2],'cats',3],'dogs',4],'dogs',1],'cats',11],'cats',1],'birds',1],'birds',12]]

我想将第二个元素为“1”的所有列表移动到列表“Test”的末尾。到目前为止,

def评分手(手):


但是,执行此操作时,我的循环跳过一个元素。我是一个新的程序员,我不知道该怎么做

我不确定python中的实现细节,但在大多数语言中,在迭代时修改列表是危险的;简单地说,跟踪你在列表中的位置的东西可能会混淆

一种可能的解决方案是存储以1结尾的元素列表,然后在第二个循环中执行所有删除/追加操作

#Setting the appropriate values for the cards and moving the ones to the back

for card in hand:
    if hand[hand.index(card)][1] == 1:
        hand.append(card)
        hand.remove(card)