Re: [Tutor] performance
Don't worry too much for the accessors, I'm pretty sure it won't degrade your performance in a noticeable way, you objects will only grow a tiny bit by adding a function to the class, all objects share the same in memory code and each one has it's own data, the function for the object is just a reference for the class function, not the memory of the function itself (I think, it would be a waste of memory otherwise). However take it with a grain of salt, do your own benchmarks, you could do a simple measure with time.time() function, or use one of the several profiling modules for python (profile, hotshot, etc). Forwarded to Tutor list, I forgot it sorry! Regards, Carlos Daniel Ruvalcaba Valenzuela On 9/16/07, Jeff Peery [EMAIL PROTECTED] wrote: Hello, I've got a quick question regarding performance of lists. I am taking measurements and building up a list of objects for each measurement. the class I created for the objects has attributes of time, numerical value, person's name who collected the sample etc. I also have functions within my class (I think they are properly named 'accessors'?) that get a piece of data within the object, for example 'self.GetSampleTime()'. I'm wondering what happens to my performance as I add more accesors to my class. How are the accesors managed? will each object in my list of objects contain the data for each accesor or do all the objects look to the sample module for the accesor? will my list of objects become huge and slow as I add more accessors? thanks. Jeff Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos. ___ 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] referencing vars()
Kent, Thanks this is exactly the solution I am looking for... so simple. On 9/15/07, Kent Johnson [EMAIL PROTECTED] wrote: John wrote: #Set up writer import csv vardict=vars() for var in vardict: if var=='allcum' or var=='alldhdt': outfile=in_path+'/'+dataset+'_'+str(var)+'.csv' writer = csv.writer(open(outfile, wb)) writer.writerows(var) I'm trying to do the above, but of course get an error because vardict is only referencing vars(), thus changes size... also, I tried vardict=[vars()], but this fails as well?? I'm not too clear what you are trying to do here. Do you want the values of the variables allcum and alldhdt? vars() gives you a dict whose keys are varible names and values are, well, the values. I think you are trying to write the contents of allcum to a file with allcum in the name? You could do it with vars like this: for var in ['allcum', 'alldhdt']: outfile=in_path+'/'+dataset+'_'+var+'.csv' writer = csv.writer(open(outfile, wb)) writer.writerows(vars()[var]) or you could iterate a list of name, value tuples directly: for name, value in [('allcum', allcum), ('alldhdt', alldhdt)]: outfile=in_path+'/'+dataset+'_'+name+'.csv' writer = csv.writer(open(outfile, wb)) writer.writerows(value) I think I prefer the second, even with the duplication of names; it feels more explicit to me. Another alternative would be to accumulate the values in a dict with keys 'allcum' and 'alldhdt'. Then you would look up in that dict instead of in vars(). HTH, Kent -- Configuration `` Plone 2.5.3-final, CMF-1.6.4, Zope (Zope 2.9.7-final, python 2.4.4, linux2), Five 1.4.1, Python 2.4.4 (#1, Jul 3 2007, 22:58:17) [GCC 4.1.1 20070105 (Red Hat 4.1.1-51)], PIL 1.1.6 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to match regular expression from right to left
王超 wrote: yes, but I mean if I have the line like this: line = 38166 us::Video_Cat::Other; us::Video_Cat::Today Show; us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf; 1003::ms://bc.wd.net/a275/video/tdy_is_.fl; I want to get the part us::MSNVideo_Cat::Other; us::MSNVideo_Cat::Today Show; us::VC_Supplier::Msnbc; but re.compile(r(us::.*) .*(1002|1003).*$) will get the 1002::ms://bc.wd.net/a275/video/tdy_is.asf; included in an lazy mode. Of course, you have asked for all the text up to the end of the string. Not sure what you mean by lazy mode... If there will always be three items you could just repeat the relevant sections of the re, something like r'(us::.*?); (us::.*?); (us::.*?);' or even r'(us::Video_Cat::.*?); (us::Video_Cat::.*?); (us::VC_Supplier::.*?);' If the number of items varies then use re.findall() with (us::.*?); The non-greedy match is not strictly needed in the first case but it is in the second. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove instances from a list
Ara Kooser [EMAIL PROTECTED] wrote Is the translation for the above line of code into pseudocode? yeast for every yeast in the list yeasts if the yeast method returned isAlive() Others have given you the solution. But note that you still have the terminology wrong. ... the yeast method returned isAlive() yeast is an object and isAlive is a method of the object. The isAlive method returns a value. Thus your sentence should have said: ... the yeast.isAlive method returned True 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
Re: [Tutor] performance
Jeff Peery [EMAIL PROTECTED] wrote I am taking measurements and building up a list of objects for each measurement. the class I created for the objects has attributes I also have functions within my class (I think they are properly named 'accessors'?) that get a piece of data within the object, You don't really need these in Python. Unless they are performing some manipulation of the data its perfectly acceptable to get the data directly from the object. Having an accessor for every attribute is a fashion quirk carried over from Java which needs it for its Javabean spec. Python OOP tends to take a much more relaxed approach to attribute access. I'm wondering what happens to my performance as I add more accesors to my class. How are the accesors managed? Adding methods to a class does not significantly affect the objects. The methods are stored as objects in a dictionary in the class and the instances just have a reference to the class. Thus all you are doing is adding method code to the class and an extra entry to the dictionary. will my list of objects become huge and slow as I add more accessors? No, the dictionary lookup is nearly constant in time regardless of size. 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
Re: [Tutor] performance
Carlos Daniel Ruvalcaba Valenzuela wrote: Don't worry too much for the accessors, I'm pretty sure it won't degrade your performance in a noticeable way, you objects will only grow a tiny bit by adding a function to the class, all objects share the same in memory code and each one has it's own data, the function for the object is just a reference for the class function, not the memory of the function itself (I think, it would be a waste of memory otherwise). However take it with a grain of salt, do your own benchmarks, you could do a simple measure with time.time() function, or use one of the several profiling modules for python (profile, hotshot, etc). Forwarded to Tutor list, I forgot it sorry! Regards, Carlos Daniel Ruvalcaba Valenzuela On 9/16/07, Jeff Peery [EMAIL PROTECTED] wrote: Hello, I've got a quick question regarding performance of lists. I am taking measurements and building up a list of objects for each measurement. the class I created for the objects has attributes of time, numerical value, person's name who collected the sample etc. I also have functions within my class (I think they are properly named 'accessors'?) that get a piece of data within the object, for example 'self.GetSampleTime()'. I'm wondering what happens to my performance as I add more accesors to my class. How are the accesors managed? will each object in my list of objects contain the data for each accesor or do all the objects look to the sample module for the accesor? will my list of objects become huge and slow as I add more accessors? thanks. AFAIK accessors are not recommended in Python, your attributes can not be hidden anyway (only by convention). Just access or set the attributes directly : myClass.myAttribute = someValue. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] class awareness of variable name
When instantiating a class, is it possible for that instance to 'know' (via the __init__ method, I suppose) the name of the variable it's been assigned to? Thanks E ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Xml reference
Hi all, Which Book is better for python parsing and reading Xml files from local machine and remote machine and also through http... Can any one Please Post the link at least ...waiting for u r Replies . Thanku:) -- Cheers, M.Srikanth Kumar, Phone no: +91-9866774007 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class awareness of variable name
Eric Abrahamsen [EMAIL PROTECTED] wrote When instantiating a class, is it possible for that instance to 'know' (via the __init__ method, I suppose) the name of the variable it's been assigned to? You could pass it in as a string but there is little point. Recall that in Python variables are just names. You can have several variables pointing at the same object, which 'name' should the instance use? class C: pass a = C() b = a d = b L = [] L.appand(a) L.append(b) a = 42 Now we have 1 instance but 4 references to it and the original name 'a' no longer points to it!. Which name should it return? Presumably you have a reason for asking this? If you tell us what you are trying to do we might be able to come up with a more conventional solution. 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
Re: [Tutor] Xml reference
I've found Python Cookbook to be a good, modern resource for parsing as well as tricks for remote pages. Link at amazon: *http://tinyurl.com/2njsd9 --Michael Original url: http://www.amazon.com/gp/product/0596007973/102-1641864-7294551?ie=UTF8tag=rowlab-20linkCode=xm2camp=1789creativeASIN=0596007973 * On 9/17/07, chinni [EMAIL PROTECTED] wrote: Hi all, Which Book is better for python parsing and reading Xml files from local machine and remote machine and also through http... Can any one Please Post the link at least ...waiting for u r Replies . Thanku:) -- Cheers, M.Srikanth Kumar, Phone no: +91-9866774007 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class awareness of variable name
On Sep 17, 2007, at 7:21 PM, Alan Gauld wrote: Eric Abrahamsen [EMAIL PROTECTED] wrote When instantiating a class, is it possible for that instance to 'know' (via the __init__ method, I suppose) the name of the variable it's been assigned to? Presumably you have a reason for asking this? If you tell us what you are trying to do we might be able to come up with a more conventional solution. To be honest, I was mostly curious, as I couldn't think of a good way to do it. I was thinking of cleaner ways of giving an instance a 'name' attribute than instance_name = Class('instance_name') in which the instance_name is repeated, and could be mistyped. I know there are problems with multiple bindings, which is why I was thinking of doing it with the __init__ method, in which case you'd get the first variable name and then none other (even if it was later unbound from the original variable). In the end, though, I was mostly curious by which mechanism you could get the variable name 'inside' the class workings. How do you pass it in as a string? Thanks!, Eric ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class awareness of variable name
Eric Abrahamsen wrote: When instantiating a class, is it possible for that instance to 'know' (via the __init__ method, I suppose) the name of the variable it's been assigned to? This is pretty hard. For one thing, the object will not be bound to a name until after __init__() is finished. Second, the object may not be bound to a name at all; it could be created and thrown away, or it could be added to a list, dict or other container, it could be a member of another object, it could be a temporary created as a function argument or the return value of a function... Even simple assignment has its complications. The assigned variable can be local or global. The assignment could be via tuple assignment in which case the value is inserted into a temporary tuple, then unpacked and assigned. In the very simple case of x = MyClass() you could probably use the stack frame to find the point of call and inspect the byte codes there to find the name... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] class awareness of variable name
This is pretty hard. For one thing, the object will not be bound to a name until after __init__() is finished. Ah, that's a good thing to know... you could probably use the stack frame to find the point of call and inspect the byte codes there to find the name... I was afraid the answer would be something like that! I will be content with the normal way of doing things then. Thanks for the explanation. Yours, Eric ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] When to use a class
I am still trying to understand when to use a class and when not to. All of the coding that I have done in the past (Python, Perl) has been procedural / functional. I would really like to do more OOP but I am not really sure when I need it. I have the following code. Is there any way that it would benefit from using a class? code #!/usr/bin/env python import string import _winreg import sys compName = sys.argv[1] x = _winreg.ConnectRegistry(compName,_winreg.HKEY_LOCAL_MACHINE) y = _winreg.OpenKey(x, rSOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion) avParent = _winreg.QueryValueEx(y,Parent)[0] _winreg.CloseKey(y) print Computer: %s \tAV Parent: %s % (compName,avParent) /code -- Thanks Eric Lake signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] When to use a class
Short sections of code are not where classes shine. Classes become much more valuable when you start to get a lot of hairy details you need to pass around. For your code, for instance, you could pass in the whole registry key you want, and have out pop a RegKey object. This would be say, usable in a system where you needed to do several operations on this object. If you were accessing multiple registry keys for instance, it would probably be nice to make a RegKey class that could be created with a handy method. If you were only accessing one key one time, a simple routine will serve you well. Realms where classes are almost always used Simulations, GUI elements, business systems of any size, many parsers, especially XML and HTML parsers. Realms where they're used much less: Basic text processing, system administration tasks, simple database systems, and number crunching apps. --Michael -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com On 9/17/07, Eric Lake [EMAIL PROTECTED] wrote: I am still trying to understand when to use a class and when not to. All of the coding that I have done in the past (Python, Perl) has been procedural / functional. I would really like to do more OOP but I am not really sure when I need it. I have the following code. Is there any way that it would benefit from using a class? code #!/usr/bin/env python import string import _winreg import sys compName = sys.argv[1] x = _winreg.ConnectRegistry(compName,_winreg.HKEY_LOCAL_MACHINE) y = _winreg.OpenKey(x, rSOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion) avParent = _winreg.QueryValueEx(y,Parent)[0] _winreg.CloseKey(y) print Computer: %s \tAV Parent: %s % (compName,avParent) /code -- Thanks Eric Lake -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (GNU/Linux) iQEVAwUBRu6u2ZLZLpR+JU3MAQp0Dgf/cMXUpmBnVM3NPQu6b2LVwEN/L5+DG0hn r3oyyVr56EIz04zl6fRqOk4NPkW0d0y5x2uvwWMCgvy64gyd9cHSrwCPxorCcf1j /71QhXA0Nx44mwJK6ahCatcfimzUF1MeykOX0oxcaAP26JDtV7eF0jYjzizsEzmE Q+2JlWzlOKrljxKL1zJLPepzubwoWFIYFmlXfYdbk2HkMCPmzPfAipEZW8WPj5xU Fu1lGWEuODSEn/+d4X6tPNlJLOAxgL01IPPUZZSso6gfjlLDHYVPTYTEUDgZIrLD XPuFpNT7tT8jQWZKg6OFjFS2P6/LVc02AYskXjegmEyMfNDZ27qLMw== =9N/n -END PGP SIGNATURE- ___ 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] class awareness of variable name
Eric Abrahamsen [EMAIL PROTECTED] wrote to do it. I was thinking of cleaner ways of giving an instance a 'name' attribute than instance_name = Class('instance_name') The thing is that you shouldn't even try! The object can have a name and that name will be constant regardless of which variable is used to reference it. Don't try to associate variables with object identifiers, they are fundamentally different things. If you want to identify an object by its name inside a program the best way to do that is via a dictionary: objects = {} objects['foo'] = MyObject('foo') Now, no matter which other variables reference it you can always go to the dictionary and pull out a reference using the object's name. And if you want to avoid typing errors use a temp: temp = MyObject('bar') objects[temp.name] = temp And if thats too much work use a factory function: def addObject(name): objects[name] = MyObject(name) I know there are problems with multiple bindings, Don't think of them as *problems*, just a different way of working :-) If you go with the flow rather than trying to make the flow go the way you want life is easier. -- 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
Re: [Tutor] When to use a class
Eric Lake [EMAIL PROTECTED] wrote I am still trying to understand when to use a class and when not to. All of the coding that I have done in the past (Python, Perl) has been procedural / functional. I would really like to do more OOP but I am not really sure when I need it. You virtually never * need* it. Sometimes it makes coding simler, but you can always do without. OOP is a different way of approaching programming, it requires a different way of thinking about your program structure. Thats why established programmers tend to find it much harder to adopt OOP than beginners with no prior experience! I have the following code. Is there any way that it would benefit from using a class? No, the code is too short, it is comparable to a method within a class. If you are mainly writing short snippets then its likely OOP will be overkill. If you did create a class then the registry might be a candidate. You might want to build a registry object woith friendlier method names than those exposed by the module. But for something this short there is no real advantage. Remember that objects are things. If you have a thing in your program then there is a possioble lass there. The actions you perform on that thing could be methods of the class. Some OOP gurus don't like the noun/verb approach but franlly I still find it the best starting point for people who are learning OOP. Write down a description of your program in English, underline the nouns and categorise them - people, places etc. The categories are potential classes, the instances are potential objects. Now look at the verbs associated with the objects you identified. These are potential operations of the classes. If there are no operations discount the class! In your example there is a computer and a registry. But there is nothing done to the computer, it is only a parameter to the registry, so discount it. The registry object is connected and queried. So you could write: class registry: def __init__(self, computer, key=None): ... def queryKey(key=None):... But I repeat, in your case the overhead of writing all the class code is bigger than your snuippet, so is only worth while if you would be reusing the registry object, either in the same program or in others that you write. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld code #!/usr/bin/env python import string You probably don;t need this, string module is pretty much redundant nowadays. import _winreg import sys compName = sys.argv[1] x = _winreg.ConnectRegistry(compName,_winreg.HKEY_LOCAL_MACHINE) y = _winreg.OpenKey(x, rSOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion) avParent = _winreg.QueryValueEx(y,Parent)[0] _winreg.CloseKey(y) print Computer: %s \tAV Parent: %s % (compName,avParent) /code ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Xml reference
chinni [EMAIL PROTECTED] wrote Which Book is better for python parsing and reading Xml files from local machine and remote machine and also through http... A lot depends on which parser you are using. David Metz Text Processing in Python is a good general text on parsing, including some HTML/XML. (Aldso available in draft online) Python Network Programming has more on the networking side(surprise!) via http. But neither uses ElementTree which is one of the best XML parsers and now standrad with python. The best bet there is the developers web page.(Try Google) There is also a book dedicated to python and XML but it gets very mixed reviews and I haven't even seen it in a store. 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
Re: [Tutor] When to use a class
Eric Lake wrote: I am still trying to understand when to use a class and when not to. All of the coding that I have done in the past (Python, Perl) has been procedural / functional. I would really like to do more OOP but I am not really sure when I need it. My take on that question is here: http://personalpages.tds.net/~kent37/stories/00014.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Finding all the letters in a string?
I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] B) Delete all the characters in the string that don't match string.letters: No idea...strip()? Thanks, Drew ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
On Mon, Sep 17, 2007 at 07:21:09PM -0400, Andrew Nelsen wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] B) Delete all the characters in the string that don't match string.letters: No idea...strip()? Thanks, Drew ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor This is what I came up with for the first part of the question. #!/usr/bin/env python # -*- coding: iso-8859-15 -*- import string lst = [] chars = '@*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^%' for x in chars: if x in string.ascii_letters: lst.append(x) for n in lst: print n, I am sure that there is probably a better way though. -- Thanks Eric Lake signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
At first I totally misread this To get the set of letters, use import string string.ascii_letters Then do what you said in your algorithm. A shorthand way to do that is filteredString = ''.join([c for c in foo if c in string.ascii_letters]) -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com On 9/17/07, Andrew Nelsen [EMAIL PROTECTED] wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] B) Delete all the characters in the string that don't match string.letters : No idea...strip()? Thanks, Drew ___ 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] Finding all the letters in a string?
Not my night...the second sentence To get the set of letters, use should read To get the filtered string.time for more Coke Zero. --Michael -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com On 9/17/07, Michael Langford [EMAIL PROTECTED] wrote: At first I totally misread this To get the set of letters, use import string string.ascii_letters Then do what you said in your algorithm. A shorthand way to do that is filteredString = ''.join([c for c in foo if c in string.ascii_letters]) -- Michael Langford Phone: 404-386-0495 Consulting: http://www.TierOneDesign.com/ Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com On 9/17/07, Andrew Nelsen [EMAIL PROTECTED] wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] B) Delete all the characters in the string that don't match string.letters: No idea...strip()? Thanks, Drew ___ 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] Finding all the letters in a string?
On Mon, Sep 17, 2007 at 07:48:56PM -0400, Michael Langford wrote: Not my night...the second sentence To get the set of letters, use should read To get the filtered string.time for more Coke Zero. --Michael On 9/17/07, Andrew Nelsen [6] [EMAIL PROTECTED] wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] B) Delete all the characters in the string that don't match string.letters: No idea...strip()? Thanks, Drew ___ I missed a part too. The original question specified alpha-numeric characters. sting.ascii.letters will only get a - z and A - Z. Would a regular expression work here with \w? -- Thanks Eric Lake signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
This seems to work to get out the alpha-numeric characters. #!/usr/bin/env python # -*- coding: iso-8859-15 -*- import re pat = re.compile('\w') lst = [] chars = '@*1^$[EMAIL PROTECTED](@2$*([EMAIL PROTECTED](*3*(c^%4^%' lst = pat.findall(chars) for x in lst: print x, -- Thanks Eric Lake signature.asc Description: Digital signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
On 18/09/2007, Andrew Nelsen [EMAIL PROTECTED] wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] Hi Andrew, First up, you should not reuse the name 'string' like that. It will lead to problems :-) You could do this: import string keepChars = string.letters + string.digits inStr = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% lst = [c for c in inStr if c in keepChars] outStr = ''.join(lst) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
Andrew Nelsen wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. Another way to do this is to use str.translate(). This method is likely faster than the other approaches though it is more obscure and the speed difference won't matter for small strings. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/59857 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
John Fouhy wrote: On 18/09/2007, Andrew Nelsen [EMAIL PROTECTED] wrote: I was wondering, recently, the most expedient way to take a string with [EMAIL PROTECTED]*] and alpha-numeric characters [ie. [EMAIL PROTECTED]@*$g@)$^@^$F] and place all of the letters in a string or list. I thought there could be obvious ways: A) Find all the letters, put them in a list, one by one. Something like (I'm not sure yet how I'd do it...): import string list = {} string = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% for x in string: if x is in string.letters? list = list + [x] Hi Andrew, First up, you should not reuse the name 'string' like that. It will lead to problems :-) You could do this: import string keepChars = string.letters + string.digits inStr = @*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^% lst = [c for c in inStr if c in keepChars] outStr = ''.join(lst) Remember how people are always saying don't use the string module unless necessary because string objects have most of the functionality built-in now This is another case of that. teststr = afdlkjal32jro3kjlkj(*^%^TUHKLJDHFKJHS(*987 print ''.join([item for item in teststr if item.isalnum()]) No imports required, may be an abuse of isalnum since (I assume) this is generally intended for use on whole strings and not on single-character strings, but either way, it works. Also note isalpha() and isdigit() for checking specific items in a string. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [tutor] Reading/Writing Ascii text file
Hello friends, I wanted a link or tutorial to help me understand how to read or write ascii text file in python. with and without using Numpy. If you have any example that would also help me understand better. thanks, Varsha Purohit, Graduate Student ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding all the letters in a string?
Thanks, everyone, for your help. It was a pretty narrow question because it's a pretty specific task, but only because I was guessing there was more than one way of shelling an acorn. My original idea was something a lot like: lst = [] chars = '@*^$[EMAIL PROTECTED](@$*([EMAIL PROTECTED](**(c^%^%' for x in chars: if x in string.ascii_letters lst.append(x) But I was guessing there was an easier way (There was.). I'm actually working through this one site called http://www.pythonchallenge.com. Seems like a nifty way to learn a little more python. Thanks, Drew ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor