Re: [Tutor] iterating data and populating a dictionary

2008-08-07 Thread W W
On Thu, Aug 7, 2008 at 12:42 AM, Shrutarshi Basu [EMAIL PROTECTED]wrote:

 If you're just going to be using numbers as dictionary keys, it might
 be simpler just to use a list structure. Dictionaries don't preserve
 order, so you'd need to write extra code if you ever need to iterate
 over it in order.


It wouldn't be any (or at least much) more difficult than looping over a
list of known/unknown length.

Known length:

for x in range(0, length):
do something with mydict[x]



Unknown length, dict keys starting at 0:

for x in range(0, len(mydict)):
do something with mydict[x]



Known length, dict keys starting at 1:

for x in range(1, (len(mydict)+1)):
do something with mydict[x]



Probably not the most elegant solution, but it does work. I can't really
think of a particular reason it would be useful, though. The main reason a
dict is useful is for key values that won't change, and aren't easily kept
track of (i.e. names). It's a lot more difficult (at least for the
interpreter, as in processor cycles) to find John Smith in a list, than to
convert it to a hash value and look it up in a hash table.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterating data and populating a dictionary

2008-08-06 Thread Shrutarshi Basu
If you're just going to be using numbers as dictionary keys, it might
be simpler just to use a list structure. Dictionaries don't preserve
order, so you'd need to write extra code if you ever need to iterate
over it in order. Since your code increments field everytime it gets a
new record you can just use it as a list index and things should be
fine. Of course you might have your own reasons for using a dict, so
it's up to you.
-- 
The ByteBaker :
http://www.bytebaker.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] iterating data and populating a dictionary

2008-08-05 Thread Bryan Fodness
i am filling a dictionary with a dictionary and my values for
isegment[field] are identical.  i can't see where i am overwriting the
previous field values.

my data looks like

Field = aa1
Index = 0.0
Value = 0.0
...
...
 Field = aa2
Index = 0.01
Value = 0.5
...


i would like to have something like,  {1: {'Value': 0.0, ...}, 2: {'Value':
0.01, ...}}

for line in file(data_file):
the_line = line.split()
if the_line:
if the_line[0] == 'Field':
field += 1
elif the_line[0] == 'Index':
index = float(the_line[-1])
dif_index = index - start_index
iindex[field] = dif_index
start_index = index
elif the_line[0] == 'Value':
segment[the_line[1]] = float(the_line[-1])*(ax/40)
isegment[field] = segment


-- 
The game of science can accurately be described as a never-ending insult to
human intelligence. - João Magueijo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterating data and populating a dictionary

2008-08-05 Thread Monika Jisswel
maybe this can help

MainDictionary = {}
N = 0
for line in open('data_file', 'r'):
if line:
N += 1
a, b = line.split( = )  # = is in between spaces whish gives you
only two variables.
if a == 'Field':
MainDictionary[N] == {}
elif a == 'Index':
MainDictionary[N]['Value'] = {b}



2008/8/5 Bryan Fodness [EMAIL PROTECTED]

 i am filling a dictionary with a dictionary and my values for
 isegment[field] are identical.  i can't see where i am overwriting the
 previous field values.

 my data looks like

 Field = aa1
 Index = 0.0
 Value = 0.0
 ...
 ...
  Field = aa2
 Index = 0.01
 Value = 0.5
 ...


 i would like to have something like,  {1: {'Value': 0.0, ...}, 2: {'Value':
 0.01, ...}}

 for line in file(data_file):
 the_line = line.split()
 if the_line:
 if the_line[0] == 'Field':
 field += 1
 elif the_line[0] == 'Index':
 index = float(the_line[-1])
 dif_index = index - start_index
 iindex[field] = dif_index
 start_index = index
 elif the_line[0] == 'Value':
 segment[the_line[1]] = float(the_line[-1])*(ax/40)
 isegment[field] = segment


 --
 The game of science can accurately be described as a never-ending insult
 to human intelligence. - João Magueijo

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterating data and populating a dictionary

2008-08-05 Thread Monika Jisswel
oops i forgot to count lines,

MainDictionary = {}
N = 0
M = 0
for line in open('data_file', 'r'):
if line:
if M  3:
N += 1
M += 1
a, b = line.split( = )  # = is in between spaces whish gives
you only two variables.
if a == 'Field':
MainDictionary[N] == {}
elif a == 'Index':
MainDictionary[N]['Value'] = {b}
else:
a, b = line.split( = )  # = is in between spaces whish gives
you only two variables.
if a == 'Field':
MainDictionary[N] == {}
elif a == 'Index':
MainDictionary[N]['Value'] = {b}
 M = 0
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterating data and populating a dictionary

2008-08-05 Thread Alan Gauld


Bryan Fodness [EMAIL PROTECTED] wrote


i am filling a dictionary with a dictionary and my values for
isegment[field] are identical.  i can't see where i am overwriting 
the

previous field values.


Show us the full error text do not just summarize.

i would like to have something like,  {1: {'Value': 0.0, ...}, 2: 
{'Value':


Describe the output you expected and what you got (if anything)


for line in file(data_file):
the_line = line.split()
if the_line:
if the_line[0] == 'Field':
field += 1
elif the_line[0] == 'Index':
index = float(the_line[-1])
dif_index = index - start_index
iindex[field] = dif_index


Here index is assigned to a floating point number.
Then it is indexed. Floats don't have indexes...

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] iterating data and populating a dictionary

2008-08-05 Thread Alan Gauld


Alan Gauld [EMAIL PROTECTED] wrote 


index = float(the_line[-1])
dif_index = index - start_index
iindex[field] = dif_index


Here index is assigned to a floating point number.
Then it is indexed. Floats don't have indexes...


Just noticed the indexed variable has a double i so not 
the same. Might be better to pick a more descriptive 
name though! :-)


Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor