[Tutor] How to verify all things are equal to one another
Suppose I have several variables, e.g.: a, b, c, d, e, f, g. I would like to be able to see if they're all the same, I don't care what the value is, as long as they're equal. If they're all equal to 0, or to "spam", or to ["cleese", "idle", "gilliam"], as long as they're the same. Is there a more pythonic way of doing this other than, if (a == b & a == c & a == d & a == e & a == f & a == g): do stuff For example, is there any kind of function: if allsame(a, b, c, d, e, f, g): do stuff I can roll my own, but I was just wondering if something already existed like this. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] map() and lambda to change class instance attribute (fwd)
Thanks Alan, that clears things up quite well. Bernard On 5/14/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > > So if I undestand you right, mapping a function with map() > > when it is a built-in function will/may be faster than a for > > loop, but if it's a custom function (ie. a def one), it will > > most likely be slower? > > That's right, a builtin function, including map itself will > be written in C and so be 'fast'. So we have the trade off > between a Python for loop calling a function or a C loop > which additionally has to build a list result. But in the > builtin case we have C code calling C code which is > much faster than Python code calling C code. > > So for a function written in Python there will be less > difference and the bulk of the time is likely to be > spent in the function calls but for builtins C to C > could give a significant benefit. > > Which is faster will depend on several other factors including > what the function returns and how easily that can be packaged > into a list. > > From my personal experience I wouldn't expect map to be > much faster than a for loop, if at all. But as Kent says > you can always profile it and test it if you really need > speed. The real issue is the map call is more obscure > than an explicit loop since map() is intended to build > a list not modify an existing list! > > Alan G. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help: space formatting for multiplication table
>The idea is to print out a multiplication table on the command line >with numbers lining up in the ones column. I want to eventually >emulate programs like top with their spacing. But this code is mostly >for my amusement. How would you make this work? > Try print string formatting using % >>>print '%5d' % 12 12 >>>print '--%5d--' % 12 -- 12-- >>>print '--%5d--' % 112 -- 112-- >>>print '--%5d--' % 1123 -- 1123-- >>>print '--%5d--' % 11235 --11235-- >>>print '--%5d--' % 112356 --112356-- >>>print '--%05d--' % 112 --00112-- http://docs.python.org/lib/typesseq-strings.html _ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help: space formatting for multiplication table
On May 14, 2005, at 20:09, Aaron Elbaz wrote: > The idea is to print out a multiplication table on the command line > with numbers lining up in the ones column. I want to eventually > emulate programs like top with their spacing. But this code is mostly > for my amusement. How would you make this work? > You may want to have a look at the string ljust() and rjust() methods. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help: space formatting for multiplication table
I've been racking my brain and am right now feeling nauseous from not being able to figure out such a simple problem. Here's the code: #r=10 #line=1 # #def spaces(r): #return r/10 # #while line-1 < r: #for i in range(r): #print str((i+1)*line) + ' '*spaces(r), #line=line+1 #print The idea is to print out a multiplication table on the command line with numbers lining up in the ones column. I want to eventually emulate programs like top with their spacing. But this code is mostly for my amusement. How would you make this work? -thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists of files
On 14 Mai 2005, [EMAIL PROTECTED] wrote: > Here's the problem - I want a list (array) of the files in a directory, > and then I want to iterate over the list testing for image-ness (with > imghdr.what()) and put all the image filenames in a global list. > > What I've tried is this: > > files = glob.glob('*.*') > > for file in files: > global pics > pics = [] > if imghdr.what(file): > # so far so good - file is a list of files in the directory > pics.append(file) > # I this this is the problem - my list only has the last > # alphabetical entry in it The problem here is that in every iteration you set the list pics to `[]'. If you wanted to solve the problem like above (not very nice; try to avoid globals if possible) you had to define `pics' outside the loop. Bernard gave you an answer how you could solve it with a simple list comprehension (just filter the output from os.listdir); it can also be written like that: filter(imghdr.what, os.listdir('.')) Just jump before to the directory you want to get searched with: os.chdir(path) If you want a more general solution which also allows to search recursively through a directory tree you could use something like that: def filter_files (root, filepred=lambda f: f, dirpred=lambda d: False): filtered= [] jn = os.path.join for path, dirs, files in os.walk(root, topdown=True): for d in dirs: if not dirpred(jn(path, d)): dirs.remove(d) filtered.extend([jn(path,f) for f in files if filepred(jn(path, f))]) return filtered The above takes two callback functions: filepred and dirpred. For every file filepred returns a true value that file gets appended to the list of returned files. `dirpred' allows you to recurse only in directories where that function returns a true value. So to have the same as above you write: filter_files('.', filepred=imghdr.what) and if you wanted to search a directory tree without e.g. the ./bin directory you could write: filter_files('.', filepred=imghdr.what, dirpred=lambda d: not d.endswith('bin')) HTH Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists of files
Hi William, First, check out the os and os.path modules. It has exactly what you need to handle files and directories. http://www.python.org/doc/2.4.1/lib/module-os.html More specifically: http://www.python.org/doc/2.4.1/lib/os-file-dir.html http://www.python.org/doc/2.4.1/lib/module-os.path.html import os # Get list of files in directory aFiles = os.listdir( ) # Create empty list to store image files aImgFiles = [] # Iterate list to collect image files for sFile in aFiles: # Split extension to see if it is an image type # This returns a list of two elements, check the last one to get the extension if os.path.splitext( sFile )[-1] == : aImgFiles.append( sFile ) You could also do that more quickly with list comprehension: aImgFiles = [ sFile for sFile in os.listdir( ) if os.path.splitext( sFile )[-1] == ] Cheers Bernard On 5/14/05, William O'Higgins <[EMAIL PROTECTED]> wrote: > Here's the problem - I want a list (array) of the files in a directory, > and then I want to iterate over the list testing for image-ness (with > imghdr.what()) and put all the image filenames in a global list. > > What I've tried is this: > > files = glob.glob('*.*') > > for file in files: > global pics > pics = [] > if imghdr.what(file): > # so far so good - file is a list of files in the directory > pics.append(file) > # I this this is the problem - my list only has the last > # alphabetical entry in it > > So there's two questions - is there a better way to create a list of > files in a directory? And, how do I populate my list to get all of the > filenames. I've also tried "pics + [file]", but that gave me an empty > list. > -- > > yours, > > William > > > BodyID:4269787.2.n.logpart (stored separately) > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Lists of files
Here's the problem - I want a list (array) of the files in a directory, and then I want to iterate over the list testing for image-ness (with imghdr.what()) and put all the image filenames in a global list. What I've tried is this: files = glob.glob('*.*') for file in files: global pics pics = [] if imghdr.what(file): # so far so good - file is a list of files in the directory pics.append(file) # I this this is the problem - my list only has the last # alphabetical entry in it So there's two questions - is there a better way to create a list of files in a directory? And, how do I populate my list to get all of the filenames. I've also tried "pics + [file]", but that gave me an empty list. -- yours, William signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor