Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
zip_longest para la lista de la izquierda siempre

Conozco la función zip (que se comprimirá de acuerdo con la lista más corta) y zip_longest (que se comprimirá de acuerdo con la lista más larga), pero ¿cómo puedo comprimir de acuerdo con la primera lista, independientemente de si es la más larga o no?

Por ejemplo:

 Input: ['a', 'b', 'c'], [1, 2] Output: [('a', 1), ('b', 2), ('c', None)]

Pero también:

 Input: ['a', 'b'], [1, 2, 3] Output: [('a', 1), ('b', 2)]

¿Existen ambas funcionalidades en una función?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede reutilizar el código python "más o menos equivalente" que se muestra en los documentos para itertools.zip_longest para hacer una versión generalizada que se comprima según la longitud del primer argumento:

 from itertools import repeat def zip_by_first(*args, fillvalue=None): # zip_by_first('ABCD', 'xy', fillvalue='-') --> Ax By C- D- # zip_by_first('ABC', 'xyzw', fillvalue='-') --> Ax By Cz if not args: return iterators = [iter(it) for it in args] while True: values = [] for i, it in enumerate(iterators): try: value = next(it) except StopIteration: if i == 0: return iterators[i] = repeat(fillvalue) value = fillvalue values.append(value) yield tuple(values)

Es posible que pueda realizar algunas pequeñas mejoras, como almacenar en caché repeat(fillvalue) o menos. El problema con esta implementación es que está escrita en Python, mientras que la mayoría de itertools usa una implementación C mucho más rápida. Puede ver los efectos de esto comparándolo con la respuesta de Kelly Bundy .

over 4 years ago · Santiago Trujillo Report

0

Aquí hay otra toma, si el objetivo es un código legible y fácil de entender:

 def zip_first(first, *rest, fillvalue=None): rest = [iter(r) for r in rest] for x in first: yield x, *(next(r, fillvalue) for r in rest)

Esto usa la forma de dos argumentos de next() para devolver el valor de relleno para todos los iterables que están agotados.

Para exactamente dos iterables, esto se puede simplificar a

 def zip_first(first, second, fillvalue=None): second = iter(second) for x in first: yield x, next(second, fillvalue)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!