Pregunta

I'm trying to build an array of tuples with first value from list with some static values.

It should be simple but I'm struggling to do this for some reason.

For example, how do I get the following:

 [(1,100,200),
  (2,100,200),
  (3,100,200),
  (4,100,200),
  (5,100,200)]

>>> zip([1,2,3,4,5],100,200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip argument #2 must support iteration
¿Fue útil?

Solución

You could use itertools.repeat to repeat the elements that you're trying to zip together.

>>> import itertools
>>> zip([1, 2, 3, 4, 5], itertools.repeat(100), itertools.repeat(200))
[(1, 100, 200), (2, 100, 200), (3, 100, 200), (4, 100, 200), (5, 100, 200)]

You could also specify the number of times you need to repeat the element. (5 in this case)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top