Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 9th, 2012, 12:57 PM
nf1_819 nf1_819 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 nf1_819 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 53 sec
Reputation 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!!

Reply With Quote
  #2  
Old November 9th, 2012, 01:52 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
  #3  
Old November 9th, 2012, 02:52 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 499 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 19 m 2 sec
Reputation Power: 63
Use code tags ...

[code ]
your Python code here
[/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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with strings in lists

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap