"""
Write a function called
of strings, L. The function must:
1. Transform the list, *in place* (without returning a new list), so that:
1.1. Starting from the beginning of the list, remove strings which are not
alphanumeric (i.e. strings that have characters other than digits and
alphabet lettrs)
1.2. The transformed list must contain at least two elements, even if they
are not alphanumeric
2. The function must use a
3. The function documentation should read:
Remove non-alphanumeric from a list, while keeping at least 2 strings
Example:
this is what i was missing in the while loop i guess...
Write a function called
list_reduction
, that takes a single argument - a listof strings, L. The function must:
1. Transform the list, *in place* (without returning a new list), so that:
1.1. Starting from the beginning of the list, remove strings which are not
alphanumeric (i.e. strings that have characters other than digits and
alphabet lettrs)
1.2. The transformed list must contain at least two elements, even if they
are not alphanumeric
2. The function must use a
while
loop3. The function documentation should read:
Remove non-alphanumeric from a list, while keeping at least 2 strings
Example:
L = ['<', 'a', 'href', '<',] list_reduction(L) -> None # New value of L -> ['a', 'href'] L = ['Hello', '$', '?' ,'!'] list_reduction(L) -> None # New value of L -> ['Hello', '!'] L = ['More', 'Than', 'Words'] list_reduction(L) -> None # New value of L -> ['More', 'Than', 'Words']
def list_reduction(*args): """ Remove non-alphanumeric from a list, while keeping at least 2 stings""" L = args[0] M = len(L) z = [] for i in range(M): x = str(L[i]) y = x.isalnum() if y == True: z.append(x) n = len(z) while (n < 2): z.append(x) print z k = [ '>', 'ab', '!' ] list_reduction(k)when i'm executing this... I'm getting a memory error: what am i doing wrong
$ python list_reduction.py
Error:Traceback (most recent call last):
File "list_reduction.py", line 47, in <module>
list_reduction(k)
File "list_reduction.py", line 43, in list_reduction
z.append(x)
MemoryError
this is what i was missing in the while loop i guess...
n+=1