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

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Smile Help with strings in lists


    Hi everyone,

    I am pretty new to Python and I am currently taking a course on intro to computer programming. We currently have an assignment on strings and lists. I'm currently out doing field work, so I haven't been able to talk to my professors and I figured I'd come here for some help!

    So here is the problem, I am given a list of names

    names = ['Bob', 'Susan', 'Mike']

    With this list, I create a function that has two variables; one variable is the list, and the other variable is an integer. The integer represents the length of each string in the list. The return value is the number of names in the list that are greater than the integer. For example:

    >>> my_function(list, integer)
    >>> my_function(names, 4)
    >>> 1 (this would be Susan)

    So the code I wrote for this goes like this:

    def my_function(list, length):

    count = 0

    for i in range(len(list)):
    if len(list[i]) > length:
    count = count + 1
    return count

    So this code seems to work properly, however, there is a second part to this problem that I need to solve.

    When I return my number, the function must also update the original list to include empty spaces in the string equal to the length that I specified in my function. For example:

    >>> names = ['Bob', 'Susan', 'Mike']
    >>> my_function(list, integer)
    >>> my_function(names, 4)
    >>> 1
    >>> names = ['Bob ', 'Susan', 'Mike']

    or if

    >>> names = ['Bob', 'Susan', 'Mike', '']
    >>> my_function(list, integer)
    >>> my_function(names, 10)
    >>> 0
    >>> names = ['Bob ', 'Susan ', 'Mike ', ' '] (for some reason the spaces are not showing up in the thread, each string now has a length of 10)

    What I am having trouble with is writing the second part of the function that adds spaces to the end of each string in the list and then updates accordingly. Can anyone help me out with this? I have no idea where to even start with this.

    Please remember that I am still really new to this!!! Thanks a lot!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Follow instructions at link in my signature to preserve forum spacing. ljust could work. Or you could use
    name+(' '*max(0,length-len(name)))

    Code:
    >>> help(''.ljust)
    Help on built-in function ljust:
    
    ljust(...)
        S.ljust(width[, fillchar]) -> string
        
        Return S left-justified in a string of length width. Padding is
        done using the specified fill character (default is a space).

    Here's a short version of the function you showed:
    Code:
    def my_function(list, length):
        return sum(length < len(OBJECT) for OBJECT in list)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Use code tags ...

    [code ]
    your Python code here
    [/code ]

    and take out the space after code
    Last edited by Dietrich; November 9th, 2012 at 02:54 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo