shuffling elements of a list
I would like to make a function that takes a list, more specificaly a list of strings, and shuffles its elements, like a pile of cards. The following is a script I tryed to make that implements pile shuffling. -- testdeck = list('qwertyuiop') def pileshuffle(DECK, NUMPILES): """Split the deck given into NUMPILES piles. Then also put the piles""" \ """ together to make the deck again.""" # Define a list of lists which is the piles PILES = [[]] * NUMPILES card = 0 pilenum = 0 while card < len(DECK): PILES[pilenum].append(DECK[card]) card += 1 if pilenum < NUMPILES: pilenum += 1 else: pilenum = 0 print PILES -- First of all, this script tells me that an index is out of range. I cannot see why this would be so. Second of all, I would like to have other methods of shuffling, prefererably riffle shuffling and just plain randomly arranging the elements of the list. I very much appreciate any help. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: shuffling elements of a list
Zhang Fan wrote: > On 30 May 2006 20:18:19 -0700, greenflame <[EMAIL PROTECTED]> wrote: > > Second of all, I would like to have > > other methods of shuffling, prefererably riffle shuffling and just > > plain randomly arranging the elements of the list. > > The random module has a `shuffle' method. It "Shuffle the sequence x > in place". > It may be help for you I am sorry but this does not help much. In my version of python (2.3) this method does not seem to exist. Also from the documentation, it seems that this method would give a random number. -- http://mail.python.org/mailman/listinfo/python-list
Re: shuffling elements of a list
Thank you all for all of your help. Also I got the shuffle function to work. Do not worry I will be back soon with more shuffling! However, I do not quite understand this DSU that you mention, although it looks useful. -- http://mail.python.org/mailman/listinfo/python-list
Making a second window with Tkinter
I have a script that will make a window that shows the text I want using Tkinter. What I need to do is to make another window popup above the current window showing other text. I tryed: --- from Tkinter imprt * root = Tk() L = Label(root, text="Blah") L.grid(row=0, column=0) # Other labels are also here but their inclusion is not so relavent. root.mainloop() sub = Tk() subL = Label(root, text="Blah") subL.grid(row=0, column=0) sub.mainloop() -- However, when I ran the script it only showed the second window after I closed the first one. After looking at the code I guess I can see why. The problem is that I do not know how to fix the issue. Thank you for you help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making a second window with Tkinter
Ok so I played with your script. Here is a script that more closely mimics what I would like to do except that the way I make the variable deckstr will be different. The only thing I am confused about is that it opens the second window in the beginning and when I click on the button, it does nothing even after I have closed the other window. from Tkinter import * from string import join root = Tk() def showdeck(deck): deckwin = Toplevel() deckstr = join(deck, "\n") Label(deckwin, text=deckstr, justify=LEFT, anchor=W, font="Courier").pack(fill=X) L = Button(root, text="Show Deck", font="Courier", command=showdeck(list('zxcvbnm'))) L.pack() root.mainloop() -- http://mail.python.org/mailman/listinfo/python-list
how to erase a variable
Is there a way to get rid of a variable as though it never existed? I know this sounds very basic but I have not come across any such methods. Also is the fact that I will have a bunch of extra variables just haning around because my use for them is over a bad thing? I will likely have on the order of 10 to 50 or so of these for the particular program I an working on at the moment. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to erase a variable
Ok thanks all! -- http://mail.python.org/mailman/listinfo/python-list
Re: Making a second window with Tkinter
What you said about why my code is wrong is still a bit fuzzy but it worked! -- http://mail.python.org/mailman/listinfo/python-list
reordering elements of a list
I am trying to reorder elements of a list and I am stuck as to what might be the best way to approach this. I have a (main) list of elements and another (ordering) list (which is may shorter, but not longer than the main list) which contains the order in which I want the elements of the main list but only as far along as the length of the ordering list. This may be confusing so I will try to give an example. Suppose the main list is: mainlist = list('qwertyuiop') Suppose the ordering list is: orderinglist = [3, 4, 2, 1] Then I am looking for a function that will take mainlist and orderinglist as arguments and return the following list: ['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p'] Also by the way the main list is always going to be a list of strings and the ordering list will be a list of numbers. Also the largest number in orderinglist will always be equal to the length of orderinglist. I hope this makes any sense. Thanks for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: reordering elements of a list
Thank you all for your replies. The only thing is I do not understand how the code is working. The following are more particular questions. Travis: Iam sorry, but I do not know what list comprehension is. Roberto: I do not understand the first half of the last line of your code. Also thank you for also teaching me to use '+' to append one list to another. This will be very useful for me. Christoph: I do not undertand the map method. Thanks again for all the help. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: reordering elements of a list
Thanks all for your help! -- http://mail.python.org/mailman/listinfo/python-list
function that modifies a string
I want to make a function that does the following. I will call it thefunc for short. >>> s = "Char" >>> thefunc(s) >>> s '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>" The problem is that if I look at the string after I apply the function to it, it is not modified. I realized that I am having issues with the scope of the variables. The string in the function, s, is local to the function and thus I am not changing the string that was inputed, but a copy. I cannot seem to figure out how to get what I want done. Thank you for your time. -- http://mail.python.org/mailman/listinfo/python-list
Re: function that modifies a string
Jason wrote: > > You cannot do what you are trying to do directly. Strings are > immutable objects. Once a string is created, that string cannot be > modified. When you operate on a string, you produce a different > string. Functions which operate on a string should return their value: > > >>> def thefunc(s): > ... return '||' + s + '>>' > ... > >>> s = 'Char' > >>> s = thefunc(s) > >>> s > '||Char>>' > > There /are/ a few hacks which will do what you want. However, if you > really need it, then you probably need to rethink your program design. > Remember, you can't change a string since a string is immutable! You > can change a variable to bind to another string. In the following > example, s gets rebound to the new string while t keeps the original > string value: > > >>> def changeString(varName): > ... globalDict = globals() > ... globalDict[varName] = '||' + globalDict[varName] + '>>' > ... return > ... > >>> s = 'Char' > >>> t = s > >>> changeString('s') > >>> s > '||Char>>' > >>> t > 'Char' > > Further note that this only affects variables in the global scope. I > hope this helps! > > --Jason Ok so let me see if I understand. The globalDict is just a dictionary containing the name of the global variables as the keys and their values as the values of the dictionary? Thus the inputed variable is treated like a global variable? -- http://mail.python.org/mailman/listinfo/python-list
can this be implemented?
First I should start with some introductory comments. When I first learned about programming, I started with BASIC, QBASIC to be more accurate. While I was at that stage, I learned about the INPUT command. I used it abundantly. Ok so now for the actual issue. I would like to implement something like the INPUT command from BASIC. I failed to find something on the python website documentation for beginners. Thank you for your time. -- http://mail.python.org/mailman/listinfo/python-list
Re: can this be implemented?
On Jun 2, 5:05 pm, Dan Bishop <[EMAIL PROTECTED]> wrote: > On Jun 2, 6:54 pm, greenflame <[EMAIL PROTECTED]> wrote: > > > First I should start with some introductory comments. > > > When I first learned about programming, I started with BASIC, QBASIC > > to be more accurate. While I was at that stage, I learned about the > > INPUT command. I used it abundantly. > > > Ok so now for the actual issue. > > > I would like to implement something like the INPUT command from BASIC. > > I failed to find something on the python website documentation for > > beginners. > > var = raw_input("Enter a value for var: ") > > That gives you a string; if you want a number, convert it by using the > int or float constructor. Thanks a lot! That helped a lot! -- http://mail.python.org/mailman/listinfo/python-list