Re: [Tutor] Question about list
Hi Hoffmann, On 11/04/06, Hoffmann [EMAIL PROTECTED] wrote: I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Have you read about for loops? The pythonic way of looping through a list is to do something like this: for item in list1: print item This will produce the same output as your code above, but is much nicer to read :-) I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements The challenge here is that your list contains a mixture of different types. For example, the len() function will tell us that ['Ted', 'Rock'] has two elements. But it would also tell us that 'spam!' has five elements, and it would raise an exception if we tried to find the length of 2. So you will need to ask python about the type of element you're looking at. One possibility that might work for you is this: for item in list1: if isinstance(item, (tuple, list)): print len(item) else: print 1 May I ask why you're doing this? This feels like a situation where you need to think clearly what your goals are before you go diving towards a solution :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
Hi Hoffman, It is often useful to use the for construct to process items in a list. e.g.: list1 = [ 'spam!', 2, ['Ted', 'Rock']] for item in list: ...print item spam! 2 ['Ted', 'Rock'] If you pass a list to the len() function, it will return the number of elenents in the list. e.g.: x = ['a', 'b', 'c'] len(x) 3 Now if you pass len() a string it will return the length of a string: y = 'hello' len(y) 5 Given your list below, len() will return what you're looking for when it encounters the third element of the list, but won't for the first and second elements. One way to solve this problem is to use the type() function to figure out if your item is a string or list and use len() as appropriate. I hope this provides enough of a hint. -mtw On Mon, Apr 10, 2006 at 03:29:23PM -0700, Hoffmann ([EMAIL PROTECTED]) wrote: Hello, I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Ok. This script will generate as the output each element of the original list, one per line: spam! 2 ['Ted', 'Rock'] I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements Could anyone, please, give me some hints? Thanks, Hoffmann __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Matthew White - District Systems Administrator Tigard/Tualatin School District 503.431.4128 The greatest thing in this world is not so much where we are, but in what direction we are moving. -Oliver Wendell Holmes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
--- John Fouhy [EMAIL PROTECTED] wrote: Hi Hoffmann, On 11/04/06, Hoffmann [EMAIL PROTECTED] wrote: I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Have you read about for loops? The pythonic way of looping through a list is to do something like this: for item in list1: print item This will produce the same output as your code above, but is much nicer to read :-) I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements The challenge here is that your list contains a mixture of different types. For example, the len() function will tell us that ['Ted', 'Rock'] has two elements. But it would also tell us that 'spam!' has five elements, and it would raise an exception if we tried to find the length of 2. So you will need to ask python about the type of element you're looking at. One possibility that might work for you is this: for item in list1: if isinstance(item, (tuple, list)): print len(item) else: print 1 May I ask why you're doing this? This feels like a situation where you need to think clearly what your goals are before you go diving towards a solution :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Hi John, This is just a version of a book exercise (How to think like a computer scientist - learning with python, by Downey, Elkner, and Meyers), page 84. Thanks, Hoffmann __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
On Mon, 10 Apr 2006, Hoffmann wrote: Hello, I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Ok. This script will generate as the output each element of the original list, one per line: spam! 2 ['Ted', 'Rock'] I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements Well, the length of spam! is 5. Lengths of strings express the number of characters. You could check to see if it's a grouping-type of element -- i.e., a list, tuple or set -- but I think your better approach is that, if this is something you need, make all of the elements lists, some of which are single-item lists; for example, instead of: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] use: list1 = [ ['spam!'], [2], ['Ted', 'Rock'] ] list1 = [ ['spam!'], [2], ['Ted', 'Rock'] ] for item in list1: ... print item, len(item) ... ['spam!'] 1 [2] 1 ['Ted', 'Rock'] 2 If your heart is set on the other approach, though, it can be done: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] for item in list1: ... if isinstance(item,(list, tuple, set)): ... print item, len(item) ... else: ... print item, 1 ... spam! 1 2 1 ['Ted', 'Rock'] 2 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
On Mon, 10 Apr 2006, Hoffmann wrote: I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements Could anyone, please, give me some hints? The problem is slightly weird, just because you need to clarify what it means to take the length of a non-list. From the examples above, it sounds like we'd like to define the length of a non-list to be one. Is that right? Can you write a function called length() that takes a thing and returns the length of that thing? ## def length(something): ... ## fill me in ## For example, we'd like to see: length(spam!) == 1 length(2) == 1 length(['Ted', 'Rock']) == 2 If you can define this you should be able to use this to solve your problem. When you're defining length(), you may find the built-in function type() useful. For example: ## type(5) type 'int' type([1, 2, 3]) type 'list' ## Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
--- Terry Carroll [EMAIL PROTECTED] wrote: On Mon, 10 Apr 2006, Hoffmann wrote: Hello, I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Ok. This script will generate as the output each element of the original list, one per line: spam! 2 ['Ted', 'Rock'] I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements Well, the length of spam! is 5. Lengths of strings express the number of characters. You could check to see if it's a grouping-type of element -- i.e., a list, tuple or set -- but I think your better approach is that, if this is something you need, make all of the elements lists, some of which are single-item lists; for example, instead of: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] use: list1 = [ ['spam!'], [2], ['Ted', 'Rock'] ] list1 = [ ['spam!'], [2], ['Ted', 'Rock'] ] for item in list1: ... print item, len(item) ... ['spam!'] 1 [2] 1 ['Ted', 'Rock'] 2 If your heart is set on the other approach, though, it can be done: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] for item in list1: ... if isinstance(item,(list, tuple, set)): ... print item, len(item) ... else: ... print item, 1 ... spam! 1 2 1 ['Ted', 'Rock'] 2 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Hi Terry, Your aproaches (mainly the last one!) answered my question. After following your last approach, I got what I was looking for. Up to the page I am (page 84 of How to think like a computer scientist - learning with Python), I didn't see that nice approach. Thanks! Hoffmann __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about list
--- Matthew White [EMAIL PROTECTED] wrote: Hi Hoffman, It is often useful to use the for construct to process items in a list. e.g.: list1 = [ 'spam!', 2, ['Ted', 'Rock']] for item in list: ...print item spam! 2 ['Ted', 'Rock'] If you pass a list to the len() function, it will return the number of elenents in the list. e.g.: x = ['a', 'b', 'c'] len(x) 3 Now if you pass len() a string it will return the length of a string: y = 'hello' len(y) 5 Given your list below, len() will return what you're looking for when it encounters the third element of the list, but won't for the first and second elements. One way to solve this problem is to use the type() function to figure out if your item is a string or list and use len() as appropriate. I hope this provides enough of a hint. -mtw On Mon, Apr 10, 2006 at 03:29:23PM -0700, Hoffmann ([EMAIL PROTECTED]) wrote: Hello, I have a list: list1 = [ 'spam!', 2, ['Ted', 'Rock'] ] and I wrote the script below: i = 0 while i len(list1): print list1[i] i += 1 Ok. This script will generate as the output each element of the original list, one per line: spam! 2 ['Ted', 'Rock'] I also would like to print the length of each element of that list: spam! = 1 element 2 = 1 element ['Ted', 'Rock'] = 2 elements Could anyone, please, give me some hints? Thanks, Hoffmann __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Matthew White - District Systems Administrator Tigard/Tualatin School District 503.431.4128 The greatest thing in this world is not so much where we are, but in what direction we are moving. -Oliver Wendell Holmes Hi Matthew, Thanks for the nice information! I am learning a lot with all of the hints you guys are sending to me. Hoffmann __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor