【问题标题】:Python's list.pop half off sale [duplicate]Python list.pop 半价促销[重复]
【发布时间】:2014-06-11 23:54:53
【问题描述】:

我试图在每次迭代中一个一个地弹出列表中的所有元素,问题是 Python 只弹出列表的上半部分,我需要它来弹出所有元素。这是我的代码:

cards = ['As', 'Ks', 'Qs', 'Js', '10s', '9s', '8s', '7s', '6s', '5s', '4s', '3s', '2s',\
        'Ad', 'Kd', 'Qd', 'Jd', '10d', '9d', '8d', '7d', '6d', '5d', '4d', '3d', '2d',\
        'Ac', 'Kc', 'Qc', 'Jc', '10c', '9c', '8c', '7c', '6c', '5c', '4c', '3c', '2c',\
        'Ah', 'Kh', 'Qh', 'Jh', '10h', '9h', '8h', '7h', '6h', '5h', '4h', '3h', '2h']

        #itertools.combinations returns a tuple and we store it in tupOnePlayerAllCombos
        tupOnePlayerAllCombos = itertools.combinations(cards, 2)

        #We need to convert out tuple to a list in order for us to use the replace() methos, tuples do not have such a method
        lstOnePlayerAllCombos = list(tupOnePlayerAllCombos)

        #There are characters we need to delete from each list item, we will store each edited list item in this list
        lstEditedOnePlayerAllCombos = []        


        #replace all useless characters in every list item (   '   and   (   and   )   and   ,   )
        for lstOnePlayerAllCombo in lstOnePlayerAllCombos:
            lstOnePlayerAllCombo = str(lstOnePlayerAllCombo).replace("'","").replace("(","").replace(")","").replace(", ","")           

            #Add edited list item to our new list
            lstEditedOnePlayerAllCombos.append(lstOnePlayerAllCombo)
            lstEditedOnePlayerAllCombos2 = lstEditedOnePlayerAllCombos

        #For every list item in our combination list
        for lstEditedOnePlayerAllCombo in lstEditedOnePlayerAllCombos:

            #We need to delete the current list item from the list, so that we dont get duplicate hands for other players
            #so we retrieve the index by searching the list with our current value and store that value as our player1 hand         
            strPlayerOneHand = lstEditedOnePlayerAllCombos2.pop(lstEditedOnePlayerAllCombos.index(lstEditedOnePlayerAllCombo))
            intLength = (len(lstEditedOnePlayerAllCombos2)-1)

            #print (lstEditedOnePlayerAllCombos)
            print("The current length of the list is " + str(intLength))

【问题讨论】:

  • 要清空整个列表吗?
  • 这似乎是XY problem。您对pop 和迭代的问题并不是您真正要解决的问题,只是您遇到的障碍。看起来你真正想做的是找到多对卡片,它们之间没有任何重复。如果您需要有关该问题的帮助(而不仅仅是 pop 问题),您应该添加有关您实际想要迭代的信息(具有给定数量玩家的游戏的手?)。可能有更直接的方法来获得您需要的特定迭代。

标签: python


【解决方案1】:

你可以像这样删除整个列表的内容

my_list = [1, 2, 3, 4, 5]
del my_list[:]
print my_list
# []

在 Python 3.x 中,你可以使用list.clear 方法,像这样

my_list = [1, 2, 3, 4, 5]
my_list.clear()
print(my_list)
# []

如果你想pop所有元素一个接一个,在每次迭代中,那么

my_list = [1, 2, 3, 4, 5]
while my_list:
    print my_list.pop()
# 5
# 4
# 3
# 2
# 1

【讨论】:

  • 对不起,我没有具体说明,我想在每次迭代中将每个元素弹出到一个变量中,并在同一次迭代中从列表中删除该元素。
  • @TrayTray 请检查更新后的答案。
  • 噢...我没有意识到他们在 py3k 中添加了list.clear。优秀。确实很棒。 (我一直想知道为什么它在 python2.x 中没有与其他内置类型保持一致......)
  • @TrayTray 很高兴能帮上忙。如果你觉得我的回答对你有帮助,你可以Accept my Answer :-)
  • 这是我第一次上论坛,抱歉……已接受
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
相关资源
最近更新 更多