Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
Rewriting Python code to remove zip and cycle functions for Javascript translation

I need some help rewriting some Python code, to exclude the zip and cycle functions I currently have in it if possible. I am still a coding novice, so I'm not sure if it's doable.

I am using a program called PsychoPy to build experiments for students, wherein we can also upload them online. For online upload there is an autoJS option, however there doesn't appear to be a JS equivalent I can find for zip and cycle.

My code is as such

for x in savedImages1:
    for y in savedImages2:
        if x == y:
            choiceImages.append(x)
            
for z in choiceImages:
    while choiceImages.count(z) < 60:
        choiceImages.append(z)

combinedList = list(zip(choiceImages, cycle(labelsWhole)))
combinedList = [list (elem) for elem in combinedList]
random.shuffle(combinedList)

Essentially what is happening here is there is a list (choiceImages) where images they have chosen previously populate this list (60 times). So if you picked image A, it would add image A to choiceImages 60 times. labelsWhole is a premade list, also consisting of 60 items, where their image choices are paired against each image in labelsWhole (e.g [A,A],[A,B],[A.,C] etc). This list is then shuffled up so the image pairs remain (e.g [A,B][B,E] etc). I zipped them as a list so the image pairs remain unaltered during the shuffling, as if I just shuffled choiceImages and labelsWhole separately I could end up with [A,B],[A,B] [B,A] [B,A] where I should only get [A,A] [A,B] [B,A] [B,B]. Hopefully this makes sense. I then later split the pairs into two separate lists, which then remain unaltered.

So I need a way to, without zip or cycle functions:

  • Pair the images with each other, so that each image is only paired against another once, so it can be shuffled and split plater.

  • Cycle through labelsWhole to add additional iterations (for example, if they selected 4 images to begin with, choiceImages would have each image * 60, so labelsWhole would need to be added to a list 4 times).

I hope I've explained this well enough. If not please let me know where it doesn't make sense, and I will try to explain it better.

Best wishes

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You may implement your homemade versions of zip and cycle.

If you check the docs you will see "roughly equivalent" versions, but they're still using some typical Python construct which may be difficult to translate to JS.

So if you cannot transfer to JS the concept of a generator you will need to have versions that return the full list, instead of yielding one element at a time.

Following are two possible implementations, I tried to make them as non-Pythonic as possible:

def myzip(iterables):
    minlen = 2**64-1
    for it in iterables:
        ln = len(it)
        if ln < minlen:
            minlen = ln
    out = []
    for j in range(minlen):
        t = []
        for it in iterables:
            t.append(it[j])
        out.append(t)
    return out

def mycycle(iterable, count):
    ln = len(iterable)
    out = []
    for j in range(count):
        out.append(iterable[j % ln])
    return out

myzip(('ABCD', 'XY'))  
[['A', 'X'], ['B', 'Y']]

mycycle('ABCD',13)
['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A']

Note that cycle repeats indefinitely, so I added a count parameter to say how many elements you want.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda