Tim Chase wrote:
> > ****************************************************************
> > a = ['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
> >
> > As an exercise, write a loop that traverses the previous list and
> > prints the length of each element. What happens if you send an
> > integer to len?
> > ****************************************************************
> >
> > for i in a:
> > print len(a[i])
> >
> > will not do.
> > the list has str, int, list, list.
> > I am expecting the output to be 1, 1, 3, 3 which are the number of
> > elements of each element of a, someone might think the result should
> > be 4, 3, 3 which is len(a), len(a[2]), len(a[3]) but how can I do both
> > thoughts with a loop?
>
> Well, first off, you've got a strange indexing going on
> there: a[i] requires that the index be an integer. You
> likely *mean*
>
> for thing in a:
> print len(thing)
>
> If so, you can just wrap it in a check:
>
> for thing in a:
> if "__len__" in dir(thing):
> print len(thing)
> else:
> print len(str(thing))
> #print 1
>
> or whatever sort of result you expect here.
>
> Or you can give it a best-effort:
>
> for thing in a:
> try:
> print len(thing)
> except TypeError:
> print 1
>
> and let exception-handling deal with it for you.
>
> Just a few ideas,
And probably what the writer of the exercise had in mind.
But I would say it's wrong. To my way of thinking, "each element"
implies recursive:
import operator
def typelen(t,offset=0):
if operator.isSequenceType(t):
print '\t'*offset,'Sequence length:',len(t)
if len(t)>1:
for i in t:
if (operator.isSequenceType(i)):
typelen(i,offset+1)
if (operator.isMappingType(i)):
typelen(i,offset+1)
if operator.isNumberType(i):
print '\t'*(offset+1),'Number length: n/a'
else:
if (operator.isMappingType(t)):
print '\t'*offset,'Mapping length:',len(t)
if operator.isNumberType(t):
print '\t'*(offset+1),'Number length: n/a'
if operator.isMappingType(t):
print '\t'*offset,'Mapping length:',len(t)
for i in t:
if (operator.isSequenceType(i)):
if len(i)>1: typelen(i,offset+1)
if operator.isNumberType(t):
print '\t'*offset,'Number length: n/a'
a=['spam!',1,['Brie','Roquefort','Pol le
Veq'],[1,2,3],{'ab':1,'abc':2}]
typelen(a)
I added a dictionary to the example since dictionaries have length
even though they are not sequences. Running it, I get:
Sequence length: 5
Sequence length: 5
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Number length: n/a
Sequence length: 3
Sequence length: 4
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 9
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 10
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 1
Sequence length: 3
Number length: n/a
Number length: n/a
Number length: n/a
Mapping length: 2
Sequence length: 2
Sequence length: 1
Sequence length: 1
Sequence length: 3
Sequence length: 1
Sequence length: 1
Sequence length: 1
--
http://mail.python.org/mailman/listinfo/python-list