py_list_intersection_finder · Python
▶ Aprender py_list_intersection_finder (Python) paso a paso · explicación de py_list_intersection_finder
Código línea a línea
Tres columnas: el código, el trozo del subject o del main que obliga a escribir esa línea, y el por qué.
py_list_intersection_finder.py
| Código | Subject | Por qué |
|---|---|---|
def list_intersection_finder(lists: list[list[int]]) -> list[int]: | def list_intersection_finder(lists: list[list[int]]) -> list[int]: | Firma del subject. |
if not lists: | list_intersection_finder([]) => [] | Sin listas no hay con qué empezar. Sin este if, lists[0] revienta. |
return [] | Vacía. | |
common = set(lists[0]) | Remove duplicates from the result | Arranco con los elementos de la primera. El set elimina los duplicados de paso. |
for other in lists[1:]: | Return elements that appear in ALL lists | Voy cruzando con las demás. |
common &= set(other) | If any list is empty, the intersection is empty | Intersección de conjuntos: me quedo con lo que está en ambos. Una lista vacía deja common vacío y ya no se recupera. |
return sorted(common) | Result should be sorted in ascending order | Los sets no tienen orden: ordeno al devolver. Aquí sorted está permitido. |