954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove objects from list

Hi! Have some problems with my code again...

I have a list "playerCardList" containing six elements where you pick out three of these. The three elements you pick should be removed from the bigger list "cardList" containing 81 elements. And after that three new elements from "cardList" would be put into "playerCardList"... One big mess, I know!

import random


class Kortlek():
    def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
        self.cardList = []
        self.playerCardList = []
        for color in colors:
            for form in forms:
                for number in numbers:
                    for grade in grades:
                        tmp= Kort(color, form, number, grade)
                        self.cardList.append(tmp)

    def spelkorten(self):
        for a in random.sample(self.cardList, 6):
            self.playerCardList.append(a)

        self.skrivUt()

        self.fraga()

    def fraga(self):

        val = "4"

        while val !="1":


            val = raw_input("Would you like to remove three cards??\n1.Yes\n2.Qiut\n")
            if val == "1":
                for kort in range(3):
                    txt = "Skriv in nummret på kort " + str(kort) + "  : "
                    kortID = int(raw_input(txt))
                    try:
                        self.playerCardList.pop(kortID-1)
                    except IndexError:
                        print "Det angivna kortet finns inte i din hög!"
                        kortID = int(raw_input(txt))
                        self.playerCardList.pop(kortID-1)

                self.addcard()


            elif val == "2":
                quit()

            else:
                print "Pick 1 or 2"
            

    def addcard(self):
        for a in random.sample(self.cardList, 3):
            self.playerCardList.append(a)

        self.skrivUt()
        self.fraga()

    def skrivUt(self):
        for b in self.playerCardList:
            b.visa()


Anyone know how to do this? :)

Swedenrock
Newbie Poster
9 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

There actually is a way to do this. Do this one item at a time, using the remove() function. I have an example below.

CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_1)
cardList.remove(CurrentItem)
CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_2)
cardList.remove(CurrentItem)
CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_3)
cardList.remove(CurrentItem)


That's not perfect, but it should give you an idea of how to do this. Hope that helps.
Cheers!

VulcanDesign
Light Poster
46 posts since Jan 2010
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: