Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: My Big Dict.

Reply
Thread Tools

RE: My Big Dict.

 
 
sismex01@hebmex.com
Guest
Posts: n/a
 
      07-02-2003
> From: [mailto]
> Sent: Miércoles, 02 de Julio de 2003 08:32 a.m.
>
> [...snippage...]
>
> d={}
> for l in file("test.txt"):
> try: i=l.index('!')
> except ValueError: continue
> d[l[:i]]=l[i+1:]
>
>


This example is *almost* ideal; how about, instead of
using .index and slices, let the computer do s'more
of your work:


D = {}
for line in file("test.txt"):
try:
k,v = line.split("!",1)
D[k] = v.strip()
except ValueError:
continue


and presto. split() takes an optional second argument
which specifies the maximum number of splits it's
allowed to perform.

Also, although I've kept your basic code form, it's
bad style to not explicitly close any opened files,
unless you absolutely know they're going to close
once the operation finishes.

-gustavo

--
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.

 
Reply With Quote
 
 
 
 
Aurélien Géron
Guest
Posts: n/a
 
      07-02-2003
> [...snippage...]
>
> d={}
> for l in file("test.txt"):
> try: i=l.index('!')
> except ValueError: continue
> d[l[:i]]=l[i+1:]
>
> [...snippage...]


Just a little note : I generally try to avoid lowercase L and uppercase i as
variable names, because in many fonts, there's barely any difference
(sometimes none at all) between l and 1 and I (resp. lowercase L, number 1
and capital i).

For example : would you be able to find out what the result of the following
code is just by looking at it? If you think you can, run it through python
and see if you were right!

l=1l
I=1+l
print l+l*I

Cya,
Aurélien


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net 0 12-26-2008 09:29 AM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net Web Controls 0 12-26-2008 06:11 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Python 0 12-24-2008 07:35 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Ruby 0 12-24-2008 05:07 AM
Big JARs = Big Problems? kk_oop@yahoo.com Java 11 09-18-2005 05:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57