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

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2

    A little help with understanding counters


    Hi i was wondering if we have a file that we looped through line by line looking like this:

    Hello
    Hi
    Bonjour

    Bye
    Aurovoir
    Bye Bye

    And we want to make a list of list of string out of this..
    How do we make a counter for this to reset.
    What type of if condition would we use.

    So for example our function looks like this:
    result = []
    def word_list(file):
    ---sub_result = []
    ---for next_line in file:
    --- ---if __________________:
    --- ---- ---- result.append(next_line)
    --- ---else:
    --- --- ---- sub_result.append(next_line/ next_ekement)

    Basically my question is that, the new sublist begins when the next line hits an empty line...i wanna know a way to make it so.. perhaps a counter that restarts everytime a blank line appears?

    Example:
    >>> word_list(open("file.txt", "r"))
    [['Hello', 'Hi', 'Bonjour'], ['Bye', 'Aurovoir', 'Bye Bye']]

    Something like this..

    Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    We already answered this in post 6.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    i know.. But it didnt really do anything.

    I dont really want the solved code and i am aware that my code didnt have a return function, but its because i was going to fix that later on.

    I just want someone to help me with the counters.

    Do we even need a counter for this is my question. Also, if we do, then to restart it, we would need to set it to 0 right?

    For example
    if sub_list == ['\n']:
    counter == 0
    This will restart the function if i am right, but i just want someone to explain it to me.

IMN logo majestic logo threadwatch logo seochat tools logo