Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Beginners question.

Reply
Thread Tools

Beginners question.

 
 
Player
Guest
Posts: n/a
 
      08-31-2004
Hi all

I am teaching myself python and so am following a few tutorials from
online.

In one of the tutorials an exercise is the following...

As an exercise, write a loop that traverses the previous list and prints
the length of each element.

Firstly I just wanted to see if I could get the length out of those elements
that were list type.
I no thats not exactly what the exercise asks for, but I thought I would
start there.
However I am stuck and cant figure out why my code is doing what it's doing.
My code is as follows....

mylist = [1, 2, ["three", "four"], "five"]
for item in mylist:
if item == type(list):
myitem = len(item)
print myitem
else:
print item

All my code seems to do is print out each element of the list on a new line.
What I had expected it to do was this....
1
2
2
'five'

but it doesn't, what it does do is this...

1
2
['three', 'four']
'five'

I expected the line,
myitem = len(item)
to issue the variable, myitem, with a value that was the length of item.
But as you cans ee it doesn't and simply ignores all that code and does the
else:
print item
part instead, misisng out the first half of the for loop.

Were am I going wrong ??

Thanks in advance for any help

M.B aka - Player -

--
*************
The Imagination may be compared to Adam's dream-
he awoke and found it truth.
John Keats.
*************


 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      08-31-2004
"Player" <> wrote in message
news:ch2c6b$5ap$...
> Firstly I just wanted to see if I could get the length out of those

elements > that were list type.
> I no thats not exactly what the exercise asks for, but I thought I would
> start there.
> However I am stuck and cant figure out why my code is doing what it's

doing.
> My code is as follows....
>
> mylist = [1, 2, ["three", "four"], "five"]
> for item in mylist:
> if item == type(list):


The "if" statement above is always false (well, at least for any list you
are likely to encounter in real life), because you are looking to see if the
item is equal to "type(list)" which is a type object. I think you meant:

if type(item) == list:

That's why your len() statement was never being reached.

> myitem = len(item)
> print myitem
> else:
> print item
>


--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.


 
Reply With Quote
 
 
 
 
Jeremy Bowers
Guest
Posts: n/a
 
      08-31-2004
On Tue, 31 Aug 2004 18:23:23 +0100, Player wrote:

> mylist = [1, 2, ["three", "four"], "five"]
> for item in mylist:
> if item == type(list):
> myitem = len(item)
> print myitem
> else:
> print item


General tip: Don't forget how much fun the interpreter is:

Python 2.3.4 (#1, Jun 8 2004, 17:41:43)
[GCC 3.3.3 20040217 (Gentoo Linux 3.3.3, propolice-3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> item = ["three", "four"] # since this is the one we wonder about
>>> item == type(list)

False
>>> # Uh-oh, there's the problem, let's see if we can get a clue:

....
>>> item

['three', 'four']
>>> type(list)

<type 'type'>
>>> # Clearly, there's something wrong here; try Russell's solution:

....
>>> type(item)

<type 'list'>
>>> list

<type 'list'>
>>> # Ah ha!

....
>>>


 
Reply With Quote
 
Player
Guest
Posts: n/a
 
      08-31-2004
[edit:snip]

Ok thanks Russell Blau I can see it now heh.

M.B -aka- Player


 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      09-01-2004
Player <> wrote:
...
> if item == type(list):


It's very unlikely that the item will equal the type of list (which is
the builtin metaclass 'type' itself). You presumably mean:

if isinstance(item, list):


Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners Question CHAZDG Wireless Networking 2 11-10-2005 12:29 AM
Beginners Guides: Website Hosting With Apache Silverstrand Front Page News 0 10-24-2005 01:44 PM
Dual port Ram - for beginners dwerdna VHDL 7 04-08-2005 06:19 AM
Beginners questions for addition Clemens Bosch VHDL 0 12-02-2004 02:20 PM
Wireless Security... for beginners!! =?Utf-8?B?SkI=?= Wireless Networking 5 11-10-2004 07:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57