splitting tables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Skip Montanaro

    #16
    Re: splitting tables


    robsom> ... I (still) don't like the "for in range()" way to build
    robsom> loops.

    That's needed only when you need a numeric index. Much of the time you can
    just iterate over a container object:

    for element in mylist:
    print element

    or

    for key in mydict:
    print (key, mydict[key])

    Skip

    Comment

    • robsom

      #17
      Re: splitting tables

      [color=blue]
      > On Wed, 11 Feb 2004 00:44:18 +0000 (UTC), robsom wrote:[color=green]
      >> I'm beginning to appreciate Python even though at first I hated this
      >> indentation thing[/color]
      >
      > Yup. Just because something seems limiting doesn't mean it is.[/color]

      Right, I'm just saying that I was used to indent when I wanted and what I
      wanted and that I missed the "closing instruction"
      But, to be fair, I've adapted much faster than I thought. Just a matter of
      getting used to it.
      [color=blue]
      > The "for index in range( size )" is mostly recommended as a way to loop
      > over a list; range( size ) will elegantly generate a sequence of indices
      > into a list of size "size".
      >
      > What is it you don't like?[/color]

      well, I just think that a kind of sintax like: for i=x to y
      is much more intuitive and easy to use. It caused me a lot of errors,
      particularly in the beginning because I wasn't getting the values I
      wanted for the index. Again is probably just a matter of getting used to
      it.
      [color=blue]
      > Because somewhere earlier in that code you must have imported the name
      > 'split' into the base namespace. It's not there to begin with (and
      > importing it is needlessly polluting the base namespace).
      > They're two different functions. One is a method of string objects, one
      > is a function you've imported from somewhere. (This confusion is partly
      > why importing symbols into the base namespace is a bad idea.)[/color]

      in fact there is an import string * instruction at the beginning.
      ok, so you suggest using the method because it results in a faster code?
      thanks

      R

      Comment

      • Ben Finney

        #18
        Re: splitting tables

        On Thu, 12 Feb 2004 00:56:22 +0000 (UTC), robsom wrote:[color=blue][color=green]
        >> On Wed, 11 Feb 2004 00:44:18 +0000 (UTC), robsom wrote:[color=darkred]
        >>> [why do str.split() and split(str) both work?][/color]
        >> Because somewhere earlier in that code you must have imported the name
        >> 'split' into the base namespace. It's not there to begin with (and
        >> importing it is needlessly polluting the base namespace).[/color]
        >
        > in fact there is an import string * instruction at the beginning.[/color]

        Yes, the "from module import *" is a deprecated usage; it pollutes the
        base namespace, which leads to confusions like this and others.

        The recommended way is to:

        import module

        module.function ()

        So, in your case, this would be:

        import string

        string.split( str )

        Then it becomes clear in each instance that the "split" function comes
        from the "string" module, and not some other arbitrary module.
        [color=blue][color=green][color=darkred]
        >>> [are they the same function?][/color]
        >> They're two different functions. One is a method of string objects,
        >> one is a function you've imported from somewhere. (This confusion is
        >> partly why importing symbols into the base namespace is a bad idea.)[/color]
        >
        > ok, so you suggest using the method because it results in a faster
        > code?[/color]

        No, I recommend using the method because it's already part of the string
        objects, and results in clearer code.

        --
        \ "It ain't so much the things we don't know that get us in |
        `\ trouble. It's the things we know that ain't so." -- Artemus |
        _o__) Ward (1834-67), U.S. journalist |
        Ben Finney <http://bignose.squidly .org/>

        Comment

        • John J. Lee

          #19
          Re: splitting tables

          Skip Montanaro <skip@pobox.com > writes:
          [color=blue]
          > robsom> ... I (still) don't like the "for in range()" way to build
          > robsom> loops.
          >
          > That's needed only when you need a numeric index. Much of the time you can
          > just iterate over a container object:
          >
          > for element in mylist:
          > print element
          >
          > or
          >
          > for key in mydict:
          > print (key, mydict[key])[/color]

          also (Python 2.3):

          for index, element in enumerate(mylis t):
          # blah, blah


          John

          Comment

          Working...