Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
November 9th, 2012, 12:57 PM
 nf1_819
Registered User

Join Date: Nov 2012
Posts: 1
Time spent in forums: 40 m 53 sec
Reputation Power: 0
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
November 9th, 2012, 01:52 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

#3
November 9th, 2012, 02:52 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 499
Time spent in forums: 4 Days 3 h 19 m 2 sec
Reputation Power: 63
Use code tags ...

[code ]
[/code ]

and take out the space after code
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : November 9th, 2012 at 02:54 PM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with strings in lists