【发布时间】:2020-06-09 20:13:25
【问题描述】:
我有一个列表,我想将带有 '1' 的元素移到列表末尾。
例如,我有一个列表: 测试 = [['dogs',2], ['cats', 3], ['dogs',4] , ['dogs', 1], ['cats', 11], ['cats',1] , ['鸟',1], ['鸟', 12]]
我想将所有以“1”作为第二个元素的列表移动到列表“Test”的末尾。到目前为止,
def score_hand(hand):
#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)
但是,执行此操作时,我的循环会跳过一个元素。我是一个新的编码员,我不确定该怎么做。
【问题讨论】:
标签: python python-3.x list loops methods