Thread: printing list?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    34
    Rep Power
    11

    printing list?


    --------1st method -------
    <k-lines> >>> a = tokenize.whitespace(text)
    <k-lines> >>> list(a)
    <k-lines> ['Hello', 'World', 'this', 'is', 'a', 'string.']
    <k-lines> >>> c = list(a)
    <k-lines> >>> print c
    <k-lines> []

    ---------2nd method ----------------
    <k-lines> >>> b = list(tokenize.whitespace(text))
    <k-lines> >>> print b
    <k-lines> ['Hello', 'World', 'this', 'is', 'a', 'string.']

    I was wondering why C is empty (1st method)? while if did (2nd mtehod) b has something? ........
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    it works for me:

    Code:
    >>> a =  ['Hello', 'World', 'this', 'is', 'a', 'string.']
    >>> list(a)
    ['Hello', 'World', 'this', 'is', 'a', 'string.']
    >>> c = list(a)
    >>> print c
    ['Hello', 'World', 'this', 'is', 'a', 'string.']
    >>>
    what is tokenize.whitespace(text) return? like what type of object is it?
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    http://python.org/doc/2.3.5/lib/module-tokenize.html
    As far as I know the tokenize module does not have a whitespace() function.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    o wow, i have no idea what i was thinking, sorry i thought it was something your wrote. Ya, i cna't find anything on a method called whitespace()
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    34
    Rep Power
    11
    http://nltk.sourceforge.net/api-1.4/index.html
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    34
    Rep Power
    11
    what is tokenize.whitespace(text) return? like what type of object is it?[/QUOTE]

    generator object...
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Oh, a gernerator? Then that's the usual behaviour, like here:
    Code:
    >>> l = (x for x in range(10))
    >>> list(l)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> c = list(l)
    >>> c
    []
    A generator "generates" the elements when they are needed. You can do this step by step using next():
    Code:
    >>> l = (x for x in range(2))
    >>> l.next()
    0
    >>> l.next()
    1
    >>> l.next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    StopIteration
    >>>
    As you can see, after the second next()-call, there are no elements left to generate. That's exactly what happened in your example. When you called list(a), all the elements were generated. When you called c = list(a), no more elements were left, so you got []

    Regards, mawe

IMN logo majestic logo threadwatch logo seochat tools logo