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

    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Just starting out....


    Good Evening All,

    I've been working in LabView, Matlab, and QBasic and I really want to get into Python but I've been waiting for a good project to get my feet wet. The other day I was trying to sort a list in LabView and I realized I could do it in Python.

    I would love some guidance in trying to make this little program.

    Here is what I have in mind:

    Start with a text file that has item separated by returns.

    Load the text file into the program

    Have the option to write a string and have it added to the top of the text (or array or whatever works)

    Then go through all the strings in the array from the first element to the last element and allow you to choose to send it to the last position or have it stay where it is.
    After both of those modes are executed I would like it to save the array back into the text file.

    Hope that makes some sense and thanks in advance for your help.

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

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Move an item to the "back" of the list
    Code:
    #  Use doctest
    #
    # $ python3 -m doctest -v p.py
    # python3 -m doctest -v p.py
    # Trying:
    #     a = list('abcdefgh')
    # Expecting nothing
    # ok
    # Trying:
    #     move_item_to_back_of_list(a, 4)
    # Expecting nothing
    # ok
    # Trying:
    #     print(a)
    # Expecting:
    #     ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'e']
    # ok
    # 1 items had no tests:
    #     p
    # 1 items passed all tests:
    #    3 tests in p.move_item_to_back_of_list
    # 3 tests in 2 items.
    # 3 passed and 0 failed.
    # Test passed.
    
    def move_item_to_back_of_list(L, item):
        '''
            >>> a = list('abcdefgh')
            >>> move_item_to_back_of_list(a, 4)
            >>> print(a)
            ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'e']
        '''
        a = L[item]
        del L[item]
        L.append(a)
    prompting for string input, valid in python 2 and in python 3.
    Code:
    $ echo Hi world | python3 -c 'import p; print(p.prompt_for_a_string("enter a string: "))'
    enter a string: Hi world
    
    $
    On second thought, you probably want to retain the new line character.


    Code:
    # reading a file
    
    with open('arrange.me.txt') as inf:
        data = inf.readlines()
    
    
    # influencing the data
    arrange(data)
    
    
    
    # write a file
    with open('arranged.txt', 'w') as ouf:
        ouf.write(optional_heading)
        ouf.write(''.join(data))



    Leaving arrange. How should we write arrange?

    Oops, I forgot to insert the prompting code. Here (Thank you emacs for long undo history!)
    Code:
    import sys
    
    def prompt_for_a_string(message):
        sys.stdout.write(message)
        return sys.stdin.readline()#.strip() # you might want to strip the newline character.  str.strip() is a sledge which is probably alright.

    Code:
    MESSAGE = '''
        In questions that follow, an answer including any "inkable"
        character means "Move the given line to the files end."
    '''
    
    def arrange(L):
        # Iterating over a changing object can be big trouble.  We need a trick.
        print(MESSAGE)
        j = 0
        for i in range(len(L)):
            if prompt_for_a_string(L[j]).strip():
                move_item_to_back_of_list(L, j)
            else:
                j += 1
    Please observe that I didn't test arrange. Do you feel more secure with arrange or with the tested move_item_to_back_of_list ? How will you choose to write your future programs?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Thanks!


    Thank you so much. I've got more than enough to get started.

    C.

IMN logo majestic logo threadwatch logo seochat tools logo