-1

for homework I need to create a function that receives 2 list of numbers and every time a number from the first list matches with one of the second one I have to add 10 to the number in the first list. Here's what I've tried.

def modifyEspecials(mylist,especials):
  return list(map(lambda n: str(int(n)+10) if map(lambda x: True if n==x else False,especials)== True else n,mylist))

if __name__ == "__main__":
  especials=[*range(39,48),*range(58,63),*range(91,97)]
  mylist= ['72', '45', '41', '56', '46', '56', '49', '45', '48', '41', '39', '46', '71', '52', '46', '56', '52', '46', '42']
  modifiedList= modifyEspecials(mylist,especials)
  print(modifiedList)

This returns the same list.

Note: I can't create variables inside modifyEspecials() in this homework and I'm only allowed to import ascii_lowercase, ascii_uppercase and functools or for/while loops

6
  • 1
    For starters mylist is a list of strings, not numbers. Commented Jun 1, 2020 at 6:05
  • 2
    I think you're making it overcomplicated. Look into list comprehensions instead of using map and lambdas Commented Jun 1, 2020 at 6:06
  • 1
    are you required to use lambda/map? Commented Jun 1, 2020 at 6:06
  • Yes, I'm required to make it overcomplicated Commented Jun 1, 2020 at 6:10
  • Mission accomplished then - mylist is all strings, it's not the same length as especials, it's unclear if you need to add 10 when two values in matching positions are the same, or only if a value from the first is in the second list at all. Your function body is very complicated, but does the same as return list(map(lambda n: str(int(n)+10) if False else n, mylist)), so it's the same as return list(map(lambda n: n, mylist)) or really return list(mylist) - and that's what it does. Commented Jun 1, 2020 at 6:16

1 Answer 1

1

So if you have to use this overcomplicated methods to do so, there is 2 mistakes you do:

  1. In the second map you compare string of int with ints
  2. The second map returns an iterator of size especials

In order to overcome the first issue just transform to int, for the second issue just sum all elements after transforming to list, if there is at least 1 True than you should add 10

See the following fix:

def modifyEspecials(mylist,especials):
  return list(map(lambda n: str(int(n)+10) if sum(list(map(lambda x: True if int(n)==x else False, especials))) > 0 else n, mylist))

if __name__ == "__main__":
  especials=[*range(39,48),*range(58,63),*range(91,97)]
  mylist= ['72', '45', '41', '56', '46', '56', '49', '45', '48', '41', '39', '46', '71', '52', '46', '56', '52', '46', '42']
  modifiedList= modifyEspecials(mylist,especials)
  print(modifiedList)

Note Although map and lambda are powerful, its not always advisable to use them where there is no need to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.