Wednesday, 15 June 2011

python - How to get a function to execute until the conditions are not sufficient -



python - How to get a function to execute until the conditions are not sufficient -

import re-create def remove_fully_correct(answer, guess): """(list,list) -> list homecoming list removes chars first list same , in same position in sec list >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) ['a','c'] """ res = copy.copy(answer) index in range(len(res)): x, y in zip(res, guess): if res[index] == guess[index]: res.remove(x) homecoming res

basically have function removes characters 1 list found in sec list, function seems remove first value found list. help appreciated

you returning within loop have 1 pass through outer loop :

import re-create def remove_fully_correct(answer, guess): """(list,list) -> list homecoming list removes chars first list same , in same position in sec list >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) ['a','c'] """ res = copy.copy(answer) index in range(len(res)): x, y in zip(res, guess): if res[index] == guess[index]: res.remove(x) homecoming res # move outside

you should utilize enumerate if lists same size:

def remove_fully_correct(answer, guess): """(list,list) -> list homecoming list removes chars first list same , in same position in sec list >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) ['a','c'] """ homecoming [ele index,ele in enumerate(answer) if ele != guess[index]] in [6]: remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) out[6]: ['a', 'c']

using zip:

def remove_fully_correct(answer, guess): """(list,list) -> list homecoming list removes chars first list same , in same position in sec list >>>remove_fully_correct(['a','b','c','d'], ['d','b','a','d']) ['a','c'] """ homecoming [a a,b in zip(answer,guess) if != b]

python for-loop

No comments:

Post a Comment