473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

convert a int to a list

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[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?
thank you
Apr 28 '06 #1
3 2396
> ************************************************** **************
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,

-tkc


Apr 28 '06 #2
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

Apr 29 '06 #3
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):


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/
Apr 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
5
by: Andrew V. Romero | last post by:
At work we have an excel file that contains the list of medications and their corresponding strengths. I would like to save the excel file as a text list and paste this list into a javascript...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
1
by: Spam Catcher | last post by:
For example, I have the following: Dim MyObjectArray() as Object = External.Function() Is it possible to convert MyObjectArray into a Generics Collection.List? Thanks.
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
1
by: pnbaocuong | last post by:
Dear All When I try to use convert function of MS sql server like this: String sql = " (employee_id = '" + employee_id + "') and ((convert(VARCHAR,w_date,103) = '" + w_date + "'))"; ...
2
by: Bart Kastermans | last post by:
I have written a little program that takes as input a text file, converts it to a list with appropriate html coding (making it into a nice table). Finally I want to upload this list as a textfile...
10
by: =?Utf-8?B?Um95?= | last post by:
What is the way to have best performance to copy a byte to a value such as long? I use BitConverter.ToInt64(binary, offset) But the performace is not good enough. I need to have the best...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.