Re: [Tutor] Str method
On 05/11/12 01:52, Oscar Benjamin wrote: On 5 November 2012 01:45, Ashley Fowler afowl...@broncos.uncfsu.edu wrote: I'm trying to initialize a list to several elements using the string method. I have some idea that you use a for loop I suppose... It will help if you work on getting your terminology clearer. Computing is a precise art, it has very specific meanings for things. From your previous thread I assume you mean that you want break a list down into its individual parts so that you can convert them to strings? To get the individual parts you do indeed need a loop - a for loop is probably best. To convert them to strings use the str() type convertor on each part. for part in myList: myString = str(part) But assuming this is still part of your previous exercise you really want to create one long string so you probably want the last line to look like myString = myString + str(part) There are more efficient ways of doing that but they might confuse you so I'll leave it with string addition for now. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str method
On 5 November 2012 01:45, Ashley Fowler afowl...@broncos.uncfsu.edu wrote: I'm trying to initialize a list to several elements using the string method. I have some idea that you use a for loop I suppose... Could anybody help me out? I'm afraid not. Your question is too vague for anyone receiving your message to know what it is you are trying to do (I have no idea what the string method is). Can you please: 1) Post the code that you tried 2) Explain what output you were hoping for 3) Show the exact output that you got (if it is an error message then please post the entire exact error message). Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str method
On 11/04/2012 08:45 PM, Ashley Fowler wrote: I'm trying to initialize a list to several elements using the string method. I have some idea that you use a for loop I suppose... Could anybody help me out? list has no string method, at least in the versions I tried.Could you perhaps restate your problem in English? What data do you have, in what kind of objects? What kind of data do you want to end up, in what kind of object? What are the rules and restrictions for how that transformation should happen? What version of Python are you using? What code do you have so far? How does it fail to meet your expectations? If it gives an error, please post the entire traceback. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str method
I have code for a Set ADT. The Set ADT is a container that stores a collection of values or elements. Below is some of my code...I have run into two problems so far. First I have to modify __init__ method so that new sets can be initialized to a list of elements. This modification adds a starred parameter, *initElements. But somehow I still need to initialize a list to several elements and I have no clue what to do. The other problem is I had to add the str method to allow a user to print the contents of the set... meaning I must construct a string from the elements of the set one by one, and with curly braces at the beginning and at the end. class Set : def __init__( self, *initElements ): self._theElements = list() def __len__( self ): return len( self._theElements ) def __contains__( self, element ): return element in self._theElements def add( self, element ): if element not in self : self._theElements.append( element ) def remove( self, element ): assert element in self, The element must be in the set. self._theElements.remove( item ) def __iter__( self ): return _SetIterator( self._theElements ) def __str__(self): for element in str(self._theElements): return element From: Oscar Benjamin [oscar.j.benja...@gmail.com] Sent: Sunday, November 04, 2012 8:52 PM To: Ashley Fowler Cc: tutor@python.org Subject: Re: [Tutor] Str method On 5 November 2012 01:45, Ashley Fowler afowl...@broncos.uncfsu.edu wrote: I'm trying to initialize a list to several elements using the string method. I have some idea that you use a for loop I suppose... Could anybody help me out? I'm afraid not. Your question is too vague for anyone receiving your message to know what it is you are trying to do (I have no idea what the string method is). Can you please: 1) Post the code that you tried 2) Explain what output you were hoping for 3) Show the exact output that you got (if it is an error message then please post the entire exact error message). Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str method
On 11/04/2012 09:01 PM, Ashley Fowler wrote: (You top-posted, so I have to remove all the historical context, or it'll remain out of order) I have code for a Set ADT. The Set ADT is a container that stores a collection of values or elements. Below is some of my code...I have run into two problems so far. First I have to modify __init__ method so that new sets can be initialized to a list of elements. This modification adds a starred parameter, *initElements. Why? How are you planning to use the data, that makes the * useful? You said you're planning to call it with a list. Where are your examples of calling code, that'll create objects from this class? But somehow I still need to initialize a list to several elements and I have no clue what to do. If you take out the star, then that's easy: self._theElements = initElements or perhaps self._theElements = initElements[:] The other problem is I had to add the str method to allow a user to print the contents of the set... meaning I must construct a string from the elements of the set one by one, and with curly braces at the beginning and at the end. You asked this in another thread, which had at least one correct answer 3 days ago. Why repeat the question if you're not going to study the replies? class Set : def __init__( self, *initElements ): self._theElements = list() def __len__( self ): return len( self._theElements ) def __contains__( self, element ): return element in self._theElements def add( self, element ): if element not in self : self._theElements.append( element ) def remove( self, element ): assert element in self, The element must be in the set. This is abuse of the assert statement. It's for finding bugs, not validating data. At the least you should do a raise here. self._theElements.remove( item ) def __iter__( self ): return _SetIterator( self._theElements ) def __str__(self): for element in str(self._theElements): return element When you have an unconditional return in a loop, it'll only happen once. So you'll return the first element of the list. See the other thread with the same subject line that you started a few days ago. snip I'm also curious what's the intended connection between this class and an actual set. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On 01/11/12 15:34, Ashley Fowler wrote: Hello I am trying to add a str method to a Set ADT implementation to allow a user to print the contents of a set. However the resulting string should look like that of a list. except I am suppose to use curly brackets to surround the elements. For an example... set1 = Set() print(set1) {} Question is how do you implement the curly brackets in my str method? Curly brackets are just characters like any other... print( '{', 42, '}' ) This is what I have so far... def __init__( self, *initElements ): self._theElements = list() def __str__(self): return self._theElements You are returning a list. But __str__() is supposed to return a string. You need to create a string representation of your data. There are many ways to do that depending on what you want it to look like or contain. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On 11/1/2012 11:34 AM, Ashley Fowler wrote: Hello I am trying to add a str method to a Set ADT implementation to allow a user to print the contents of a set. However the resulting string should look like that of a list. except I am suppose to use curly brackets to surround the elements. For an example... set1 = Set() print(set1) {} Question is how do you implement the curly brackets in my str method? This is what I have so far... def __init__( self, *initElements ): self._theElements = list() def __str__(self): return self._theElements Please include the class statement. Give us an example of the desired output when theElementsis not empty. Why not create theElements as a set to start with? what is Set ADT? is it important that we know that? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On 01/11/2012 15:34, Ashley Fowler wrote: Hello I am trying to add a str method to a Set ADT implementation to allow a user to print the contents of a set. However the resulting string should look like that of a list. except I am suppose to use curly brackets to surround the elements. For an example... set1 = Set() Please tell us what this Set() is, then we'll attempt to answer your questions. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On Thu, Nov 1, 2012 at 4:12 PM, bob gailer bgai...@gmail.com wrote: Why not create theElements as a set to start with? what is Set ADT? is it important that we know that? I suppose it's an implementation of the set abstract data type (i.e. operations such as add, union, difference): http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On 02/11/12 02:34, Ashley Fowler wrote: Question is how do you implement the curly brackets in my str method? This is what I have so far... def __init__( self, *initElements ): self._theElements = list() def __str__(self): return self._theElements __str__ should return a string, not a list. Since _theElements is a list, you cannot rightly return that. You could convert that to a string first: s = str(self._theElements) and then replace the square brackets [ ] with curly brackets: s = s.replace([, {).replace(], }) return s Another way is to build the string yourself: s = ', '.join(str(item) for item in self._theElements) return '{' + s + '}' -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Str Method
On Thu, Nov 1, 2012 at 8:19 PM, Steven D'Aprano st...@pearwood.info wrote: s = str(self._theElements) s = s.replace([, {).replace(], }) return s Another way is to build the string yourself: s = ', '.join(str(item) for item in self._theElements) return '{' + s + '}' Or s = str(self._theElements) return {%s} % s[1:-1] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor