convert a int to a list

G

Gary Wessle

Hi

can type conversion work to convert an int to a list?
I am trying to solve an problem in one tutorial.


****************************************************************
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)

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?


thank you
 
T

Tim Chase

****************************************************************
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)

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 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,

-tkc
 
M

mensanator

Tim said:
****************************************************************
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)

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 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
 
J

James Stroud

Tim said:
****************************************************************
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)

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
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):


I think the one and only one way to express this is

if hasattr(thing, "__len__"):



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top