Finding the first letter of each string in a list

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 19
Reputation: wotthe2000 is an unknown quantity at this point 
Solved Threads: 0
wotthe2000's Avatar
wotthe2000 wotthe2000 is offline Offline
Newbie Poster

Finding the first letter of each string in a list

 
0
  #1
Jan 8th, 2009
Hi
How do you find out the first letter or number of string in a list?

I have a list and I want to find whether any of the strings in the list start with a capital or little C.

The code I have is:

def isCMD(self):
	for word in msgstrlist:
	    if msgstrlist[0][0] == 'C':
		print word
	else:
	    print "Not CMD message

I'm having a problem as it is only looking at the first word in the list. How can I change it so that it looks at all words?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Finding the first letter of each string in a list

 
0
  #2
Jan 8th, 2009
You are almost there:
  1. def isCMD(msgstrlist):
  2. for word in msgstrlist:
  3. if word[0].upper() == 'C':
  4. print word
  5. else:
  6. print "Not CMD message"
  7.  
  8. msgstrlist = ['Ox', 'Dog', 'Cow', 'mouse', 'cat']
  9.  
  10. isCMD(msgstrlist)
  11.  
  12. """
  13. my result -->
  14. Not CMD message
  15. Not CMD message
  16. Cow
  17. Not CMD message
  18. cat
  19. """
Note:
Looks like you are using a mix of tabs and spaces for indentations in your code, very bad habit!
Last edited by sneekula; Jan 8th, 2009 at 10:50 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 19
Reputation: wotthe2000 is an unknown quantity at this point 
Solved Threads: 0
wotthe2000's Avatar
wotthe2000 wotthe2000 is offline Offline
Newbie Poster

Re: Finding the first letter of each string in a list

 
0
  #3
Jan 8th, 2009
Thanks for that. I am now trying to find the strings that don't start with the letters C, R or I. My code is:
def isOTHER(self):
	for word in msgstrlist:
	    if word[0].upper() not 'C' or 'R' or 'I'
		print "Other message has been found"
	    else:
		print "No other messages were found"
but it doesnt seem to be working. I have tried using not and or truth tests but not sure if I'm doing it right.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster

Re: Finding the first letter of each string in a list

 
0
  #4
Jan 8th, 2009
try this...
  1. badletters = ['C', 'R', 'I']
  2.  
  3. # later on in your program...
  4.  
  5. if word[0].upper() not in badletters:
  6. print "Other message has been found"
  7. else:
  8. print "No other messages were found"
Last edited by mn_kthompson; Jan 8th, 2009 at 11:51 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: ammalu is an unknown quantity at this point 
Solved Threads: 0
ammalu ammalu is offline Offline
Newbie Poster
 
-1
  #5
Oct 12th, 2009
please help me to develop a program in C for the given problems?
1. Read the string and print the first letters of word?
2. Read the array elements and find the greater and smallest after sorting it?
3. Read the string and print the digits and vowels ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
Oct 12th, 2009
Originally Posted by ammalu View Post
please help me to develop a program in C for the given problems?
1. Read the string and print the first letters of word?
2. Read the array elements and find the greater and smallest after sorting it?
3. Read the string and print the digits and vowels ?
You are in the wrong forum. Many of us went away from low level C to high level Python to solve such problems easily.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC