-3

I wrote this code to sum a number and its reverse:

    #sum the multiple of the first number and its reversed

    zipped_lists = zip(primo, reversedList)
    res = [x + y for (x, y) in zipped_lists]
    print (res)

    #search common values in "res" and "secondo"

    common_list = set(res).intersection(secondo)

Now that I isolated significant numbers, I need to return to the original number listed in primo. I've no idea how to do that. Please, help me :(

petezurich
  • 9,280
  • 9
  • 43
  • 57

1 Answers1

2

You need to keep track of x against its corresponding value of x + y. Instead of checking for the presence in secondo after you create res, you could do the filtering in a single comprehension.

res = [
    x
    for x, y in zip(primo, reversedList)
    if (x + y) in secondo
]
flakes
  • 21,558
  • 8
  • 41
  • 88
  • Writing that, it doesn't work: – Andrea Valsesia Apr 01 '22 at 19:49
  • ```#sum the multiple of the first number and its reversed zipped_lists = zip(primo, reversedList) res = [x + y for (x, y) in zipped_lists] print (res) #search common values in "res" and "secondo" res = [ x for x, y in zip(primo, reversedList) if (x + y) in secondo ] print() print() if len(res) == 0: print ("No results found") else: print ("The numbers are: ", res)``` – Andrea Valsesia Apr 01 '22 at 19:49
  • @AndreaValsesia could you provide an example of `primo`, `secondo` and `reversedList` where this fails? – flakes Apr 01 '22 at 19:53
  • If there aren't matches, the script works. But, when there are common values, the script interrupts after printing ```reversedList``` – Andrea Valsesia Apr 01 '22 at 20:17