0

Here is the problem:
a function that takes two strings and removes from the first string any character that appears in the second string. eg. if the first string is “IamLearningPython” and the second string is “aeiou” the result is “mLrnngPythn”.

I've written the code for this, but I can not figure out the error:
Can any one tell me what is the error with this code:

def attenuate(sequence1, sequence2):
	newSeq = []
	n = len(sequence2)
	for m in range(len(sequence1)):
		print(m)
		while(n in range(len(sequence2)) and sequence1[m] != sequence2[n]):
			'''checks for the condn of matched string if no match found then append the string to the list newSeq '''
			if n == len(sequence2):
				entry = sequence1[m]
				newSeq.append(entry)
				
			n =+1
	return newSeq
		
def main():
	
	return 0

if __name__ == '__main__':
	inp1 = str(input('please give your input: '))
	'''IamLearningPython'''
	seq1 = list(inp1)	
	inp2 = str(input('please give next input: '))
	'''aeiou'''
	seq2 = list(inp2)
	newSeq1 = attenuate(seq1,seq2)
	print(newSeq1)
	
	
	main()
4
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by pyTony
0

Wow. That's kind of overkill for this simple task.

A oneliner:

>>> [s for s in sequence1 if s.lower() not in set(s2.lower() for s2 in sequence2)]


You set n = len(sequence2) then you make a loop with n in range(len(sequence2)), which will never be true.

Edited by slate: n/a

0

You did not state any error so there is no question to answer. In Python we generally use the "in" operator instead of iterating. Also, sets will allow you to get the difference between string2 and string1 which is what you want.

# Pseudo code 

list_2 = []
for ch in "IamLearningPython":
    if ch not in string_2:
        add it to list_2
join list_2 and print

Edited by woooee: n/a

0

Here is the trivial solution, probably you have some rules, what you can use and what not?

>>> def attenuate(sequence1, without):
	return ''.join(c for c in sequence1 if c.lower() not in without)

>>> attenuate('IamLearningPython', 'aeiou')
'mLrnngPythn'
>>>

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.