Re: [Tutor] silly remedial question
Magnus Lycka
magnus at thinkware.se
Tue Nov 25 11:03:17 EST 2003
Hi Kevin!
If you have lots of similar objects that you are going
to treat in a uniform way, don't give them a variable
each. Put them in a list or a dictionary.
E.g.:
d = {'input_1': [234, 234, 123],
'input_2': [21,345,12],
...
}
input_names = d.keys()
input_names.sort()
for input_name in input_names:
print input_name, '=', d[input_name], ',', f(d[input_name])
Using a list, you'll only access them via integer numbers. Using
a dictionary you will access them via names, but you can't preserve
the order you placed them into the dictionary. Dictionaries are
basically unordered.
(See http://www.python.org/doc/current/lib/typesmapping.html).
To preserve both a name and an order, you could use a tuple in
a list:
l = [('input_1', [234, 234, 123]),
('input_2', [21,345,12]),
...
]
for input_name, value in l:
print input_name, '=', value, ',', f(value)
-----Ursprungligt meddelande-----
Från: kevin parks <kpp9c at virginia.edu>
Skickat: 2003-11-24 09:02:53
Till: tutor at python.org
Ämne: [Tutor] silly remedial question
> Hi folks!
>
> I am trying to do something that seems rather simple, but perhaps is
> something i haven't done before or forgot...
>
> I have a function that does something to sequence and returns a
> sequence. Now i call that function a bunch of times one a whole bunch
> of lists and i get my output which is very nice. Problem is, that i
> want to make that output more humanly readable form by first printing
> the name of the list variable followed by a ' = ' and then the items in
> the list.
>
> so if sequence input_01 = [22, 25, 31] and i write something that
> returns the mod 12 equivalent of each item in a new list [10, 1, 7]
>
> i can print [22, 25, 31] and [10, 1, 7]
> but how can i get it to say :
>
> input_01 = [22, 25, 31], [10, 1, 7]
>
> Without saying:
>
> print 'input_01 =', input_01
>
> for each input list.
>
> I can print the input and output lists no problem, but how do you tell
> python to print the variable itself.. I don't want to do this by hand
> as i have a huge bunch of list that i have to do this to that are
> already sitting in a text file.
>
> Not sure i made this clear or not...
>
>
> cheers,
>
> kevin
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus at thinkware.se
More information about the Tutor
mailing list