How do you program in Python?
My question isn't as all-encompassing as the subject would suggest... I am almost a Python newbie, but I have discovered that I don't get along with IDLE, as i can't work out how to run and rerun a routine without undue messing about. What I would really like is something like an old-style BASIC interpreter, in which I could list, modify and test-run sections of code, to see the effects of tweaks, without having to save it each time, or re-typing it over and over (I haven't even worked out how to cut and paste effectively in the IDLE environment). I see lots of alternate IDEs etc, but which would allow me the simple interface that I have described? - I really don't know about IDEs in general, and I suspect I would be out of my depth with one of those. Thanks, and feel free to mock ;) -- http://mail.python.org/mailman/listinfo/python-list
string methods
I am an abject newbie, so mock away (actually no-one ever does that in this group..) Anyway, I want to replace one character in a string, based in that character's position in the string. For example if I wanted to replace the 4th character in 'foobar' (the b)with the contents of another string, newchar, what would be the easiest way? I know this touches on immutability etc, but I can't find string methods to return the first 3 characters, and then the last 2 characters, which I could concatenate with newchar to make a new string. I know the string methods are there, but can't find it in any docs, and just want to check the syntax, unless there is an easier way. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
calling functions
This is the first time I have tried out functions (is that the main way of making subroutines in Python?) Anyway, my function, mutate, below #make a child string by randomly changing one character of the parent Def mutate(): newnum=random.randrange(27) if newnum==0: gene=' ' else: gene=chr(newnum+96) position=random.randrange(len(target)) child=parent[:position-1]+gene+parent[position+1:] mutate() The trouble is when I later (as in further down the code) attempt to retrieve the value of gene I get an error saying that gene is undefined. It works fine when I don't have the routine defined as a function. - the IF- Else structure means gene must have a value of ' ' or 'a' to 'z'. It seems that the line: mutate() is not invoking the function, but why not? Thanks again - this group is great. I despair of ever being able to contribute though :-( -- http://mail.python.org/mailman/listinfo/python-list
newbie wants to compile python list of filenames in selected directories
Hi, I am new at Python, and very rusty at the one language I was good at, which was BASIC. I want to write a script to compare filenames in chosen directories, on windows machines. Ideally it would compose a list of strings of all the filenames in the directories, and those directories would be chosable by the user of the script. I am quite happy to do my own legwork on this , I realise it is simple stuff, but can anyone point me in the right direction to start? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie wants to compile python list of filenames in selected directories
M.E.Farmer wrote: anthonyberet wrote: Hi, I am new at Python, and very rusty at the one language I was good at, which was BASIC. I want to write a script to compare filenames in chosen directories, on windows machines. Ideally it would compose a list of strings of all the filenames in the directories, and those directories would be chosable by the user of the script. I am quite happy to do my own legwork on this , I realise it is simple stuff, but can anyone point me in the right direction to start? Thanks Cool! I like your attitude ;) Others have given you a good start with os. In case you don't know os has a great many path manipulation methods. Always try to use them so you can insulate yourself from cross platform path nightmares. A few of the highlights are: ### split a path py> parts = os.path.split(r'c:\windows\media\ding.wav') py> print parts ('c:\\windows\\media', 'ding.wav') ### join a path and part and do it right on any platform py> path = os.path.join('c:\\windows\\media', 'ding.wav') 'c:\\windows\\media\\ding.wav' ### get basename of the file py> base = os.path.basename('c:\\windows\\media\\ding.wav') py> print base 'ding.wav' Plus many more, be sure to study the os module if you are doing any path manipulations. Ok and now for something sorta different( alright, I was really bored ) . def glob_dir(dir): . """Return a list of *.py* (.py, .pyc, .pyo, .pyw) .files from a given directory. . """ . import glob, os . # Get a list of files that match *.py* . GLOB_PATTERN = os.path.join(dir, "*.[p][y]*") . pathlist = glob.glob(GLOB_PATTERN) . return pathlist . . def list_dir(dir): . ''' Another way to filter a dir ''' . import os . pathlist = os.listdir(dir) . # Now filter out all but py and pyw . filterlist = [x for x in pathlist .if x.endswith('.py') .or x.endswith('.pyw')] .return filterlist hth, Thanks to everyone who responded - I feel on my way now, just being able to tap 'os python' and 'lisdir python' into Google has netted me plenty of reading material - but I would have taken a long time to find that out without your tips! I will surely come back, but not until I get I get stuck :) -- http://mail.python.org/mailman/listinfo/python-list
string methods (warning, newbie)
Is there a string mething to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' If not then how best to approach this? I have some complicated plan to cut the string into individual characters and then concatenate a new string with the ones that return true with the .isalpha string method. Is there an easier way? -- http://mail.python.org/mailman/listinfo/python-list
Re: string methods (warning, newbie)
anthonyberet wrote: Is there a string mething [method] to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' erm, no it wouldn't, it would be 'TheBeatleshelpTickettoride', but you get me, I am sure. If not then how best to approach this? I have some complicated plan to cut the string into individual characters and then concatenate a new string with the ones that return true with the .isalpha string method. Is there an easier way? -- http://mail.python.org/mailman/listinfo/python-list
Re: string methods (warning, newbie)
Jimmy Retzlaff wrote: Anthonyberet wrote: Is there a string mething to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' If not then how best to approach this? I have some complicated plan to cut the string into individual characters and then concatenate a new string with the ones that return true with the .isalpha string method. Is there an easier way? The approach you are considering may be easier than you think: filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride') 'TheBeatleshelpTickettoride' Thanks very much - that's the level of knowledge of Python that I just don't have yet - everything I try to do seems to have a much easier way, that I haven't encountered yet :) I shall read up on the elements of your code to understand exactly what it is doing. -- http://mail.python.org/mailman/listinfo/python-list
TKinter
So, is it pronounced 'Tee-Kinter', or 'Tee-Kay-Inter'? I don't want to appear as a dork down the pub. -- http://mail.python.org/mailman/listinfo/python-list
Re: TKinter
Steve Holden wrote: anthonyberet wrote: So, is it pronounced 'Tee-Kinter', or 'Tee-Kay-Inter'? I don't want to appear as a dork down the pub. If anyone down your pub knows enough about Python to understand what TKinter is I very much doubt they'll be rude enough to call you a dork for displaying your ignorance. that's-my-kind-of-pub-ly y'rs - steve I have never recovered from the time I said 'Lye-Nux' and 'Ess-Kyoo-Ell' in the same evening ;|) -- http://mail.python.org/mailman/listinfo/python-list
2-dimensional data structures
Hello again - rather a newbie here... I want to work on a sudoku brute-forcer, just for fun. I am considering different strategies, but first I need to decide on the data-structure to use for the progress/solution grid. This being a square, I would have used a 9x9 2-dimensional array in my teenage years back in the 80's, using BASIC. What is the equivalent way to store data in python? - It isn't obvious to me how to do it with lists. 'scuse me for being thick - but give me a little pointer and I will do the rest. -- http://mail.python.org/mailman/listinfo/python-list
Re: 2-dimensional data structures
Tim Chase wrote: >> I want to work on a sudoku brute-forcer, just for fun. > > > Well, as everybody seems to be doing these (self included...), the > sudoku solver may become the "hello world" of the new world :) > >> What is the equivalent way to store data in python? - It isn't obvious >> to me how to do it with lists. > > > Several other answers have crossed the list. I've done it using a > dictionary of tuples: > > grid = {} > for row in range(1,10): > for col in range(1,10): > grid[(row,col)] = value > > item = grid[(3,2)] > > etc. > > Seemed fairly quick and worked for me. > Thanks for the advice (to everyone in the thread). I think I will go with nested lists. However, I am running into a conceptual problem. My approach will be firstly to remove all the impossible digits for a square by searching the row and column for other occurances. However, I wondering how to approach the search of the nine regions of the grid. I am thinking of producing another nested list, again 9x9 to store the contents of each region, and to update this after each pass through -and update of- the main grid (row and column). I am not sure how to most efficiently identify which region any given square on the grid is actually in - any thoughts, for those that have done this? - I don't want a massive list of IF conditionals if I can avoid it. -- http://mail.python.org/mailman/listinfo/python-list