Anyone has a nice "view_var" procedure ?
Gabriel Genellina
gagsl-py at yahoo.com.ar
Tue Jan 16 22:22:43 EST 2007
At Tuesday 16/1/2007 20:13, Stef Mientki wrote:
>Ok, here's my current solution,
>probably not very Pythonian, (so suggestions are welcome)
>but it works.
It appears that you're a number crunching guy - you absolutely ignore
all types except numeric ones! :)
>(Too bad I can get the name of the var into a function ;-)
I don't understand this, but if you are saying that you can't get the
name of the variable you're showing - just pass the name to the
function: def pm(V, name)
> # count the occurances of the different types
> N_int,N_float,N_complex,N_list,N_tuple,N_array,N_unknown =
>0,0,0,0,0,0,0
Ouch, seven counters... your guts should be saying that this is
candidate for a more structured type... what about a dictionary
indexed by the element type?
count = {}
> for i in range(len(V)):
You're going to iterate along all the items in V, not along its indices...
> if type(V[i]) == int: N_int += 1
> elif type(V[i]) == float: N_float += 1
> elif ...
Using a dict:
for item in V:
t = type(item)
try: count[t] = count[t]+1
except IndexError: count[t] = 1
(That is: if the type is found in the dictionary, increment the
count; if not, this is the first time: set the count to 1)
> # print the occurances of the different types
> # and count the number of types that can easily be displayed
> if type(V)==list: line = 'list:'
> else: line = 'tuple:'
> N = 0
> if N_int > 0: line = line + ' N_Int=' + str(N_int) ; N
>+= 1
> if N_float > 0: line = line + ' N_Float=' + str(N_float) ; N
>+= 1
We'll replace all of this with:
for key,value in count:
line += ' %s=%d' % (key, value)
> if N == 1: line += ' == Homogeneous =='
if len(count)==1: line += ' == Homogeneous =='
> print line
--
Gabriel Genellina
Softlab SRL
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list