-5

Suppose we are given a list X of integer arguments of some function and a list Y of integer values of this function of the same size as X . We need to construct a sequence of pairs corresponding the square of this function, i.e., (X[0],Y[0]2),(X[1],Y[1]2),…

Hint USE ZIP

from typing import Iterator
X = [1,2,3]
Y = [5,6,7]
S : Iterator[tuple[int, int]] = YOUR_EXPRESSION
assert set(S)=={(1,25), (2,36), (3,49)}

My solution is the following below. However, it does not use zip. Can someone rewrite with the zip function?

S = { (X[i], Y[i]**2) for i in range(len(X)) }
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Emilia Delizia
  • 333
  • 3
  • 14
  • 1
    What would be the benefit for you if we rewrite your homework? Did you check [the docs for zip()](https://docs.python.org/3/library/functions.html#zip), does it ring any bells? – buran Dec 01 '21 at 13:32
  • zip zips 2 list together, and yes I have read it, I cannot work it out, so I am asking for solution that I can study – Emilia Delizia Dec 01 '21 at 13:33
  • @EmiliaDelizia try `list(zip(X,Y))` what does it give you? How can you work from there to iterate? (NB. the `list` is only here to consume the generator, so don't use it in the end) – mozway Dec 01 '21 at 13:35
  • I have looked at the book and all possible examples, I cannot work it out from these examples, that is why I am asking... – Emilia Delizia Dec 01 '21 at 13:35
  • 1
    Even in the docs there is perfect example – buran Dec 01 '21 at 13:36
  • @EmiliaDelizia check my comment above, if you don't manage ping me and I'll give you the answer – mozway Dec 01 '21 at 13:36
  • 1
    Note that a set is not itself an iterator (it's iter_able_) - your attempt fails type checking ([playground](https://mypy-play.net/?mypy=latest&python=3.10&gist=418c4fea6005f049a9116da06019013a)). And the question asks for a _sequence_ of pairs then types it as an _iterator_, which isn't the same thing. – jonrsharpe Dec 01 '21 at 13:37
  • what did you type? – mozway Dec 01 '21 at 13:39
  • You'd only get that error if you'd previously assigned a set to either `list` or `zip` - don't shadow [built-in functions](https://docs.python.org/3/library/functions.html). – jonrsharpe Dec 01 '21 at 13:43
  • @jonrsharpe I told OP to test `list(zip(X,Y))` to see what `zip` is doing ;) – mozway Dec 01 '21 at 13:44
  • ok this is what I got [(1, 5), (2, 6), (3, 7)] as expected – Emilia Delizia Dec 01 '21 at 13:44
  • @mozway that in itself wouldn't be a problem if they hadn't also assigned something over one of those functions. – jonrsharpe Dec 01 '21 at 13:45
  • I provided an answer to help you move on with this… (if someone wants to close again, I can't since I already reopened: dups are https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel and https://stackoverflow.com/questions/13704860/zip-lists-in-python ) – mozway Dec 01 '21 at 13:47

1 Answers1

-1

Looks like you're really struggling with this despite an apparent good faith effort.

So here is the answer:

from typing import Iterator
X = [1,2,3]
Y = [5,6,7]
S : Iterator[tuple[int, int]] = ((a,b**2) for a,b in zip(X,Y))
assert set(S)=={(1,25), (2,36), (3,49)}

zip forms pairs like [(1, 5), (2, 6), (3, 7)], so you can iterate on them using a loop or list comprehension.

mozway
  • 194,879
  • 13
  • 39
  • 75
  • your solution works on colab, but on codio where it is automatically corrected, it gives out this Your solution is not correct! from typing import Iterator X=[1,2,3] Y=[5,6,7] S:Iterator[tuple[int, int]]=S = [(a,b**2) for a,b in zip(X,Y)] assert set(S)=={(1,25),(2,36),(3,49)} invalid syntax (, line 4) – Emilia Delizia Dec 01 '21 at 13:50
  • It should be `Tuple` (the type hint) not `tuple` (actual tuple constructor) – mozway Dec 01 '21 at 13:51
  • 3
    Not since Python 3.9, at which point `typing.Tuple` etc. were deprecated: https://www.python.org/dev/peps/pep-0585/. And a list, like a set, is **not** an iterator. They're both iterable, but that isn't the same thing. – jonrsharpe Dec 01 '21 at 16:09
  • @jonrsharpe you're right I forgot this PEP, fixed it. TBH, I had not really paid attention to the typing, mostly answered because OP was struggling with `zip` – mozway Dec 01 '21 at 16:26