Re: [Tutor] Lists on the fly?
Hi guys, I am reading and doing examples from Python Web Programming and Have a question about the dictionary: counter = {} file = open("d:\myfile.txt") # the time has come the walrus said while 1: line = file.readline() if line == "": break for w in line.split(): if counter.has_key(w): counter[w] += 1 else: counter[w] = 1 file.close() words = counter.keys() words.sort() wor w in words: print w, counter{w} Output is: Come 1 Has 1 Said 1 The 2 Time 1 Walaus 1 Okay, I understand counter is set up as a dictionary and a dictionary has a key and a value. How does the dictionary get built? I'm not following the logic in how the key, the actual word, is having a value assigned. Thanks, SteveO -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Friday, December 22, 2006 10:38 AM To: Carlos Cc: tutor@python.org Subject: Re: [Tutor] Lists on the fly? Carlos wrote: > Hello, > > I am wondering if it is possible to create lists on the fly. The > script that I'm working on needs a number of parameters, one of those > is population, and this corresponds to the number of solutions that a > genetic algorithm generates on each generation (iteration). The thing > is that I need to generate one list for each population member and > then append the corresponding population members to that particular list. > > This is only so you can have an idea: > > for i in range(5): > 'list_%i' % (i) = [] > > or: > > for i in range(5): > lista_+'%i' % (i) = [] > > :-[ > > Is this possible? It is possible, using exec, but it is not the right solution to your problem. You want a bunch of lists each associated with a name. The way to do this is put the lists in a dict; the name is the key, the list is the value. Your example would look like this: lists = {} for i in range(5): lists['list_%i' % (i)] = [] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists on the fly?
Thank you! I makes sense now and shows me I need to research more on file methods. -Original Message- From: Bob Gailer [mailto:[EMAIL PROTECTED] Sent: Friday, December 22, 2006 11:46 AM To: Steve Oldner Cc: tutor@python.org Subject: Re: [Tutor] Lists on the fly? Steve Oldner wrote: > Hi guys, > > I am reading and doing examples from Python Web Programming and Have a > question about the dictionary: > > counter = {} > file = open("d:\myfile.txt") # the time has come the walrus said while > 1: > line = file.readline() > if line == "": > break > for w in line.split(): > if counter.has_key(w): > counter[w] += 1 > else: > counter[w] = 1 > file.close() > words = counter.keys() > words.sort() > wor w in words: > print w, counter{w} > > Output is: > > Come 1 > Has 1 > Said 1 > The 2 > Time 1 > Walaus 1 > > > Okay, I understand counter is set up as a dictionary and a dictionary > has a key and a value. > How does the dictionary get built? I'm not following the logic in how > the key, the actual word, is having a value assigned. > counter[w] = 1 # if w is not a key in the dictionary, this assignment adds it, with a value of 1 > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting python from a DOS prompt from any directory?
I am learning Python on the office computer which is networked, and am not allowed to change defaults (programmers aren't allowed to do system admin stuff, heck, we can't even move our PC's or monitors). I've got PYTHON installed in d:\python25. So at the DOS prompt, g:\ type in d:\ Then at the d:\ type in CD python25, which changes it to d:\python25. >From there, it's just python mystuff.py to run my programs. From: [EMAIL PROTECTED] on behalf of Alan Gauld Sent: Sun 12/31/2006 2:42 AM To: tutor@python.org Subject: Re: [Tutor] Starting python from a DOS prompt from any directory? "Daniel McQuay" <[EMAIL PROTECTED]> wrote > from a DOS prompt. i am used to running python from a linux box > where you > can just type "python" or "python24" from a shell prompt and the > python > shell executes from any directory. > testing code. now i am using a windows xp media center edition > laptop with > python 2.5 installed and when i go to run and then type "cmd" and > then type > "python" from the directory where the run "cmd" command drops me it > says > 'python' is not a recognized as an internal or external command. You need to set up your PATH environment variable to include the python directory. You do this on XP(not so sure about Media Centre!) via the MyComputer->Properties->Advanced->Environment Variables route Once there you need to find the PATH variable and edit it to add the folder where Python lives (look at the properties of the shortcut to Python that you normally use to stat it). HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting python from a DOS prompt from any directory?
Dittos, guys. Alan, I work for a state government, so I'm not suppose to download PYTHON without submitting reasons and getting permissions. I'm an applications programmer working on SAP (ASAP programmer), and we have some COBOL and Cold Fusion also. So we are pretty compartmentalized. Anyway thanks for the tip and I'll use it next time. Thanks, Steve From: [EMAIL PROTECTED] on behalf of Daniel McQuay Sent: Sun 12/31/2006 5:43 PM To: Alan Gauld Cc: tutor@python.org Subject: Re: [Tutor] Starting python from a DOS prompt from any directory? yeah, you know what i totally didn't think about setting the environmental values (yeah Media Center is the same as XP Pro). i guess i should of known that. geeze now i feel like a moron. however, i didn't know about that quick little DOS trick. thanks a lot guys for such a quick response and pointing out the obvious. this has got to be the best and most friendly list ever. happy new year to you all, On 12/31/06, Alan Gauld <[EMAIL PROTECTED]> wrote: "Steve Oldner" <[EMAIL PROTECTED]> wrote > change defaults (programmers aren't allowed to do system > admin stuff, heck, we can't even move our PC's or monitors). You can just type in the PATH statement every time you start DOS PATH= %PATH%;D:\Python25 And it will have the same effect. You can even create a Batch file and put it into somewhere your PATH can see D:\Python25\python %1 %2 %3 %4 %5 %6 %7 %8 %9 should work. But how did you install Python if you can't change the system? If you have access to install programs you have access to set environment variables, at least for yourself! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Daniel McQuay boxster.homelinux.org <http://boxster.homelinux.org/> H: 814.825.0847 M: 814-341-9013 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting python from a DOS prompt from any directory?
Sorry for the delay, and the confusion. C and D are the local PC dir, where C has the network and mandatory programs installed. D is my working DIR. SO PYTHON is installed locally for me. From: [EMAIL PROTECTED] on behalf of Bob Gailer Sent: Mon 1/1/2007 4:11 PM To: Alan Gauld Cc: tutor@python.org Subject: Re: [Tutor] Starting python from a DOS prompt from any directory? Alan Gauld wrote: > "Steve Oldner" <[EMAIL PROTECTED]> wrote > >> Alan, I work for a state government, so I'm not suppose to >> download PYTHON without submitting reasons and >> getting permissions. >> > > Sure I understand that, but it looks from your post that > you have somehow managed to install Python. As I recall Steve said at the beginning that he ran Python from a network drive. > If your > account has permissions to install any software then > you probably have permissions to set at least USER > level Environmenent Variables (You may not be able to > set them at SYSTEM level) > > >> I'm an applications programmer working on SAP >> > > I know the kind of set up, we have similar teams of > folks working on Siebel and Oracle. > > Alan G. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MONEY MATTERS
My 2 cents worth is to set up something separate, it'll just keep the principles and ideas clearer. While I have my own ideas about the "million dollar application", I would like a forum where I can just listen and learn any type of ideas without having a worry that someone will 'steal' my idea and beat me to the market. I work for a state government supporting a SAP HR application, so anything I learn from this list is not directly applicable to my job. However, the ideas and logic to solve various PYTHON solutions is a big benefit in expanding my search for alternative solutions. And one day, I will have free time to work on my "million dollar app." (LOL!) Thanks, Steve Oldner -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kirk Bailey Sent: Wednesday, March 21, 2007 10:14 PM To: tutor@python.org Subject: [Tutor] MONEY MATTERS ok, SHOULD THIS BE A THREAD OR A SEPERATE LIST? Many of ius are independants, and write code for the love of it- or to promote and sell independantly. So possibly a thread discussing ways to turn useful code into moiney is a useful idea. If the wish of the list is a thread here, we can do that, or we can start a seperate list for it. What are the wishes of this list? -- Salute! -Kirk Bailey Think +-+ | BOX | +-+ knihT Fnord. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor