RE: My Big Dict.

Discussion in 'Python' started by sismex01@hebmex.com, Jul 2, 2003.

  1. Guest

    > From: [mailto:p]
    > 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.
     
    , Jul 2, 2003
    #1
    1. Advertisements

  2. > [...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
     
    Aurélien Géron, Jul 2, 2003
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    593
    Alexander Kozlovsky
    May 22, 2006
  2. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,614
    Kent Johnson
    Dec 15, 2006
  3. Almad
    Replies:
    8
    Views:
    606
    Terry Reedy
    Dec 14, 2006
  4. Drew
    Replies:
    19
    Views:
    1,610
    Duncan Booth
    Mar 15, 2007
  5. Shaguf
    Replies:
    0
    Views:
    780
    Shaguf
    Dec 24, 2008
  6. Shaguf
    Replies:
    0
    Views:
    672
    Shaguf
    Dec 26, 2008
  7. Shaguf
    Replies:
    0
    Views:
    428
    Shaguf
    Dec 26, 2008
  8. Shaguf
    Replies:
    0
    Views:
    429
    Shaguf
    Dec 24, 2008
Loading...