Need help with lists...

This is the place for queries that don't fit in any of the other categories.

Need help with lists...

Postby kadoza » Sat Sep 14, 2013 2:54 am

Hey everybody. My problem is that I have this code below which works fine as is. I want to add one more feature to it before I have to turn it in for class. I would like to add a final statement that lists the letters of the alphabet that were not used in the user input. I set lists for the vowels, the consonants, and the entire alphabet. Now I need to convert the input string to a list and remove that list from the alphabet list. I just don't know how.


Code: Select all
a = raw_input("Enter a string:").lower()
vowels = "aeiouy"
consonants = "bcdfghjklmnpqrstvwxz"
alphabet = "abcdefghijklmnopqrstuvwxyz"
nv = 0
nc = 0
nl = 0
nz = 0
for char in a:
     if char in alphabet:
         nl += 1
     if char in vowels:
         nv += 1
     if char in consonants:
         nc += 1
for char in alphabet:
    if char not in a:
        nz += 1
print()
print a
print("Number of Vowels in your statement is"),nv
print("Number of Consonants in your statementis"), nc
print("Total Letters in your statement is"), nl
print("Total number of letters in the alphabet you did not use is"), nz


The last line should be something along the lines of... print("Letters not used in your statement:"),listofunusedletters

Any help is appreciated. Thanks!
Last edited by Yoriz on Sat Sep 14, 2013 12:08 pm, edited 1 time in total.
Reason: First post lock
kadoza
 
Posts: 3
Joined: Sat Sep 14, 2013 2:46 am

Re: Need help with lists...

Postby Yoriz » Sat Sep 14, 2013 12:16 pm

In this part of your code you are counting which characters are not used.
Code: Select all
for char in alphabet:
    if char not in a:
        nz += 1

Make a Modified version of this so you first create a empty list,
then as you loop through instead of adding to the count,
append the char to the list if it is not in a.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Need help with lists...

Postby kadoza » Sat Sep 14, 2013 3:35 pm

Can't seem to get the Syntax right. This is what I'm trying based on what you have suggested. I created a blank list then attempted to append char not used in a into the list.... just prints the whole alphabet...

Code: Select all
unused = []
for char in a:
     if char in alphabet:
         nl += 1
     if char in vowels:
         nv += 1
     if char in consonants:
         nc += 1
for char in alphabet:
    if char not in a:
        nz += 1
    if char not in a:
        unused.append

print unused
kadoza
 
Posts: 3
Joined: Sat Sep 14, 2013 2:46 am

Re: Need help with lists...

Postby Yoriz » Sat Sep 14, 2013 4:07 pm

Here is a few examples of ways to do it.

Code: Select all
alphabet = 'abcdefghijklmnopqrstuvwxyz'
chars_input = 'acegiklptxz'
unused_chars = []

for character in alphabet:
    if character not in chars_input:
        unused_chars.append(character)

print unused_chars

unused_chars = [char for char in alphabet if char not in chars_input]
print unused_chars

unused_chars = list(filter(lambda char: char not in chars_input, alphabet))
print unused_chars

Output
Code: Select all
['b', 'd', 'f', 'h', 'j', 'm', 'n', 'o', 'q', 'r', 's', 'u', 'v', 'w', 'y']
['b', 'd', 'f', 'h', 'j', 'm', 'n', 'o', 'q', 'r', 's', 'u', 'v', 'w', 'y']
['b', 'd', 'f', 'h', 'j', 'm', 'n', 'o', 'q', 'r', 's', 'u', 'v', 'w', 'y']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 836
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: dirkaulo and 4 guests