473,840 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the the best and easiest way to remove elements from a list?

3 New Member
Hi!
I'm working on a solitaire game, with a different card deck. First I create a big list with 81 elements (cards in this case), cardList. From this list, six cards are ramdomly picked and put into another list, playerCardList, and printed on the court.

Once the cards are printed the player is asked to remove three cards. I would like these cards to be removed from cardList so they don't appear again. I would also like them to be put into a new list where I can compare them to one another.

Expand|Select|Wrap|Line Numbers
  1. class Kortlek():
  2.     def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
  3.         self.cardList = []
  4.         self.playerCardList = []
  5.         for color in colors:
  6.             for form in forms:
  7.                 for number in numbers:
  8.                     for grade in grades:
  9.                         tmp= Kort(color, form, number, grade)
  10.                         self.cardList.append(tmp)
  11.  
  12.     def cards(self):
  13.         for a in random.sample(self.cardList, 6):
  14.             self.playerCardList.append(a)
  15.  
  16.         self.write()
  17.  
  18.         self.question()
  19.  
  20.     def question(self):
  21.         val = raw_input("Would you like to remove three cards?\n1.Yes\n2.Quit\n")
  22.         if val == "1":
  23.             for kort in range(3):
  24.                 txt = "Write the number of card " + str(card) + ":\n"
  25.                 kortID = int(raw_input(txt))
  26.                 try:
  27.                     self.playerCardList.pop(kortID-1)
  28.                 except IndexError:
  29.                     print "That card is not in your pile"
  30.                     kortID = int(raw_input(txt))
  31.                     self.playerCardList.pop(kortID-1)
  32.  
  33.  
  34.             self.addCard()
  35.  
  36.         elif val == "2":
  37.             quit()
  38.  
  39.         else:
  40.             print "Pick either 1 or 2"
  41.  
  42.  
  43.  
  44.     def addCard(self):
  45.         for a in random.sample(self.cardList, 3):
  46.             self.playerCardList.append(a)
  47.  
  48.         self.write()
  49.         self.question()
  50.  
  51.     def write(self):
  52.         for b in self.playerCardList:
  53.             b.visa()
  54.  
  55. def cardShuffle():
  56.     card = Kortlek(["blue   ", "yellow ", "red    "],["triangel", "circle  ", "square  "],[1, 2, 3],[1, 2, 3])
  57.     card.cards()
  58.  
  59.  
  60.  
Thankful for any help! :)
Dec 5 '10 #1
2 2075
Sean Pedersen
30 New Member
the pop(elementNumb er) removes and returns an item at offset elementNumber
Dec 5 '10 #2
Johan Novak
3 New Member
Expand|Select|Wrap|Line Numbers
  1. # -*- coding: utf-8 -*-
  2. import random
  3.  
  4.  
  5. class Kortlek():
  6.     def __init__(self,colors=[], forms=[], numbers=[], grades=[]):
  7.         self.cardList = []
  8.         self.playerCardList = []
  9.         for color in colors:
  10.             for form in forms:
  11.                 for number in numbers:
  12.                     for grade in grades:
  13.                         tmp= Kort(color, form, number, grade)
  14.                         self.cardList.append(tmp)
  15.  
  16.     def spelkorten(self):
  17.         for a in random.sample(self.cardList, 6):
  18.             self.playerCardList.append(a)
  19.  
  20.         self.skrivUt()
  21.  
  22.         self.fraga()
  23.  
  24.  
  25.     def kortval():
  26.         kortvalen=[]
  27.  
  28.  
  29.     def fraga(self):
  30.         val = raw_input("Vill du ta bort tre korta blalba??\n1.Ja\n2.Avsluta\n")
  31.         if val == "1":
  32.             for kort in range(3):
  33.                 txt = "Skriv in nummret på kort " + str(kort) + ":\n"
  34.                 kortID = int(raw_input(txt))
  35.                 try:
  36.                     self.playerCardList.remove(kortID-1)
  37.                 except IndexError:
  38.                     print "Det angivna kortet finns inte i din hög!"
  39.                     kortID = int(raw_input(txt))
  40.                     self.playerCardList.remove(kortID-1)
  41.  
  42.  
  43.             #CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_1)
  44.  
  45.             #cardList.remove(CurrentItem)
  46.  
  47.             #CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_2)
  48.  
  49.             #cardList.remove(CurrentItem)
  50.  
  51.             #CurrentItem = playerCardList(INDEX_NUMBER_OF_ITEM_3)
  52.  
  53.             #cardList.remove(CurrentItem)
  54.  
  55.             self.laggTill()
  56.  
  57.         elif val == "2":
  58.             quit()
  59.  
  60.         else:
  61.             print "Välj aningen 1 eller 2"
  62.  
  63.  
  64.  
  65.     def laggTill(self):
  66.         for a in random.sample(self.cardList, 3):
  67.             self.playerCardList.append(a)
  68.  
  69.         self.skrivUt()
  70.         self.fraga()
  71.  
  72.     def skrivUt(self):
  73.         for b in self.playerCardList:
  74.             b.visa()
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. class Kort():
  82.     def __init__(self,color,form,number,grade):
  83.         self.color = color
  84.         self.form = form
  85.         self.number = number
  86.         self.grade = grade
  87.     def visa(self):
  88.         text = str(self.color) + "\t"
  89.         text += str(self.form) + "\t"
  90.         text += str(self.number) + "\t"
  91.         text += str(self.grade)
  92.         print text
  93.  
  94.  
  95.  
  96. def spelregler():
  97.     print " "
  98.     print "1. Vad går spelet ut på?"
  99.     print "2. Hur får jag poäng?"
  100.     print "3. Tillbaka till huvudmenyn\n"
  101.     spelregelsVal = raw_input("Vilket alternativ väljer du?")
  102.  
  103.     if spelregelsVal == "1":
  104.         print "\nFörst kommer sex olika kort att slumpas ut på spelplanen.\nSpelet går sedan ut på att du ska välja ut tre av\ndessa kort som sedan kommer att jämföras med varann."
  105.         spelregler()         
  106.  
  107.     elif spelregelsVal == "2":
  108.         print "\nTvå poäng ges om du väljer ut \nTre päng...."
  109.         spelregler()
  110.  
  111.     elif spelregelsVal == "3":
  112.         meny()
  113.  
  114.  
  115.     else:
  116.         print "Välj antingen val 1, 2 eller 3"
  117.  
  118.  
  119.  
  120.  
  121. def kortutdelning():
  122.     kort = Kortlek(["blue   ", "yellow ", "red    "],["triangel", "circle  ", "square  "],[1, 2, 3],[1, 2, 3])
  123.     kort.spelkorten()
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. def fraga1(fragan):
  131.     return raw_input (fragan)
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. def meny():
  146.     print "\n" * 2 + "*" * 80
  147.     print "Hej och välkommen till patiens med en annorlunda kortlek"
  148.     print "*" * 80 + "\n" * 2
  149.     print "1. Börja spela?"
  150.     print "2. Hur spelar jag?"
  151.     print "3. Avsluta?"
  152.     print " "
  153.     return fraga1("Vad väljer du? ")
  154.  
  155. val = "1"
  156.  
  157. while val != "3":
  158.     val = meny()    
  159.     if val == "1":
  160.         kortutdelning()
  161.  
  162.         val2 = raw_input("Vill du spela igen? Ja/Nej")
  163.  
  164.         val2 = "Ja"
  165.  
  166.         while val2 != "Ja":
  167.  
  168.             if val2 =="Ja":
  169.                 kortutdelning()
  170.                 print "Då kör vi! /n"
  171.  
  172.             elif val2 == "Nej":
  173.                 quit()
  174.                 print "På återseende"
  175.  
  176.             else:
  177.                 print "Välj antingen Ja eller Nej!"
  178.  
  179.  
  180.     elif val == "2":
  181.         spelregler()
  182.  
  183.  
  184.     elif val == "3":
  185.         print "På återseende"
  186.         quit()
  187.  
  188.     else:
  189.         print "Välj antingen val 1 eller 2"
  190.  
  191.  
  192.  
  193. meny()
  194.  
Dec 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2753
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
3
2629
by: Zorpiedoman | last post by:
Horay! I have just put the finishing touches on a new User Control... The" Jelly Button" I created a setup program which runs fine. I see the .dll nicely in the GAC. How come it does not show up in my Add/Remove componenets list when I try to add it to the toolbar?
2
2186
by: Daniel | last post by:
Is is possible to remove elements from the middle of a System.Collections.Queue? For the most part i just use endqueue and dequeue but there are points where i know that i no longer need certain elements that could be in the middle of the queue that i would like to clean out of the queue. however, my queue size is gigabytes large and i cant afford the time to reallocate the entire queue just to remove a middle element. do i need to write...
3
1823
by: Girish Sahani | last post by:
Hi, I am trying to convert a list of pairs (l4) to list l5 by removing those pairs from l4 which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = pairList = ,,,,,,,,]
5
1324
by: Girish Sahani | last post by:
Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = pairList = ,,,,,,,,]
5
2656
by: Wing | last post by:
Hello, I am thinking an efficient way to remove all the elements (says they are less than 10) from a container vector<int>. Any suggestion? Thank you.
9
7919
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
56
5221
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be checked on each iteration, since the Count changes as you delete elements. I would think it is guaranteed to be computed each time, and not cached. So, is this the best way?
2
2387
by: Angus | last post by:
Hello I have a vector<int(aRemovecoll) which is a list of the indexes to be removed from another vector. The other vecotr contains an object - I will call it SomeObject. So the other vecotr is a vector<SomeObject>. I have created a function like this so far: UINT uNumElements = aRemoveColl.GetSize();
0
9702
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10607
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10302
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9444
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7023
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5686
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3139
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.