[Tutor] How to traverse a dictionary

Zakir Hussain zakir@nortelnetworks.com
Mon, 23 Oct 2000 09:09:28 -0400


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C03CF2.79BC7540
Content-Type: text/plain;
	charset="iso-8859-1"

I have a dictionary with keys that are different names of sessions and each
key contains START TIME and END TIME
I want to go through dictionary and perform some calculations. I can't
figure out how to go through the dictionary from the start 
to end 

Here is the example

#This is where I am creating the dictionary
def process_new_dictionary(self):
            if not(self.sessions.has_key(self.current_entry[5])):
                    splitted_time = split(self.current_entry[0], '.')
                    t = [splitted_time]
                    self.sessions[self.current_entry[5]]= {'start':t,
'end':t}
                    
            if (self.sessions.has_key(self.current_entry[5])):
                    splitted_time2 = split(self.current_entry[0], '.')
                    t2 = [splitted_time2]
                    self.sessions[self.current_entry[5]]['end'] = t2


This is where I am trying to do the calculation but I want to go through the
entire dictionary and calculate the time 
for each session. I don't know if I should use a while loop or something
else and how to use it.
If you can help me with that I will be very thakful


def calc_time(self):
if (start_hour > end_hour):
            temp_hour = end_hour + 24
            total_hour = total_hour + (temp_hour - start_hour)


print 'total hour', total_hour



------_=_NextPart_001_01C03CF2.79BC7540
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2652.35">
<TITLE>How to traverse a dictionary</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2 FACE=3D"Arial">I have a dictionary with keys that are =
different names of sessions and each key contains START TIME and END =
TIME</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">I want to go through dictionary and =
perform some calculations. I can't figure out how to go through the =
dictionary from the start </FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Arial">to end </FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">Here is the example</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Arial">#This is where I am creating the =
dictionary</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">def =
process_new_dictionary(self):</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; if not(self.sessions.has_key(self.current_entry[5])):</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; splitted_time =
=3D split(self.current_entry[0], '.')</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t =3D =
[splitted_time]</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
self.sessions[self.current_entry[5]]=3D {'start':t, 'end':t}</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; if (self.sessions.has_key(self.current_entry[5])):</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
splitted_time2 =3D split(self.current_entry[0], '.')</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t2 =3D =
[splitted_time2]</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
self.sessions[self.current_entry[5]]['end'] =3D t2</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Arial">This is where I am trying to do the =
calculation but I want to go through the entire dictionary and =
calculate the time </FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">for each session. I don't know if I =
should use a while loop or something else and how to use it.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">If you can help me with that I will =
be very thakful</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Arial">def calc_time(self):</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Arial">if (start_hour &gt; end_hour):</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; temp_hour =3D end_hour + 24</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp; total_hour =3D total_hour + (temp_hour - start_hour)</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Arial">print 'total hour', total_hour</FONT>
</P>
<BR>

</BODY>
</HTML>
------_=_NextPart_001_01C03CF2.79BC7540--