October 28th, 2004, 11:00 AM
-
Help me out please!
Hello to all of you wazzzzzzaaaaaaa
I need some help on python programming, would appreciate it alot!
I've made two *.py files, one that is called labb2funktioner.py only has two functions where I define what is VOCALES (a, e, i,o,u, y etc etc..) and the other function in that same file is for consonants.
Now the thing is I'm gonna create a python file called VISKNING.py and in that file I want it to ask the user to Write something:
when he does, the program will re-write his text but delete the vocales. Like this:
Får jag viska ditt namn?
Fr jg vsk dtt nmn?
Visst får du det
Vsst fr d dt
The thing is I don't know how to code it so that the program DELETES the vocales.. the rest is all good though. I need some help on how to make hte program pick out all the vocales in the text and then delete them.
Thanks in advance,
Micheal
October 28th, 2004, 11:31 AM
-
There is a trivial solution for this problem. First, you should define two lists (instead of two functions) as global variables for the vowels and the consonants like this :
Code:
vowels = [ 'a', 'A', 'e', 'E', 'i', 'I', ... ]
consonants = [ 'b', 'B', 'c', 'C', ... ]
Then, the function that deletes the vowels from an input stream should look like this :
Code:
def DeleteVowels( input_stream ) :
new_stream = "" # This will hold the result
for char in input_stream :
if char in consonants :
new_stream += char
return new_stream
Note that this solution is certainly not optimal (you'd better find an implementation that uses functions from the string module or C functions) but if performance isn't a matter for you, this should work well enough.
October 28th, 2004, 11:58 AM
-
This should give you the results your after, it works a little differently than Zoso's example. Primarily the removeVowels() function appends to a list rather than using string concatenation, to improve performance when working with large strings.
Code:
>>> vowels = 'aeiouy'
>>> def removeVowels(originalString):
output = []
for letter in originalString:
if letter.lower() not in vowels:
output.append(letter)
return ''.join(output)
>>> removeVowels('Help me out please!')
'Hlp m t pls!'
>>>
>>>
>>>
>>>
>>> def replaceVowels(theString):
for letter in vowels:
theString = theString.replace(letter, '')
return theString
>>> replaceVowels('Help me out please!')
'Hlp m t pls!'
>>>
>>> theString = 'Help me out please!'
>>>
>>> [l for l in theString if l not in vowels]
['H', 'l', 'p', ' ', 'm', ' ', 't', ' ', 'p', 'l', 's', '!']
>>> ''.join([l for l in theString if l not in vowels])
'Hlp m t pls!'
>>>
All these examples will give you the same results but work slightly differently
. For preformance though I would go with the first function.
Hope this helps,
Mark.
Last edited by netytan; October 28th, 2004 at 12:23 PM.
Reason: vowels missing.
October 28th, 2004, 12:04 PM
-
Code:
import sys
from labb2funktioner import isVowel
text = raw_input("Please type something: ")
for character in text:
if not isVowel(character):
sys.stdout.write(character)
You don't need both isVowel and isConsonant...
I do "not isVowel" so it prints out other characters like spaces and returns. If you just did "if isConsonant(character)" it would miss spaces and so on.)
Or
Code:
>>> filter(lambda c: not (c in 'aeiouAEIOU'), "Hello World")
'Hll Wrld'
Or
Code:
>>> import re
>>> re.sub("[aeiouAEIOU]", "", "Hello World")
'Hll Wrld'
>>>
:P
October 29th, 2004, 01:24 AM
-
Thanks for the help!! Really appreciated!
However since this is a test I have to do it in a specific way, like this: this is the code in my labb2funktioner.py
-------
#coding: iso-8859-1
from string import *
def vokal(bokstav):
bokstav=lower(bokstav)
lista="aeiouåäö"
leta = lista.find(bokstav)
if leta >=0:
return True
else:
return False
def konsonant(bokstav):
bokstav=lower(bokstav)
lista="zxcvbnmsdfghjklqwrtp"
leta = lista.find(bokstav)
if leta >=0:
return True
else:
return False
-------
now in my viskning.py i've come so far:
-----
#coding: iso-8859-1
from labb2funktioner import *
bokstav = raw_input("Mata in text: \n")
if vokal(bokstav):
for i in range(bokstav):
print i
-------
But I don't know how to pick out the vocales and delete them??
Thanks,