“Python spłaszcza listę list” Kod odpowiedzi

Python, jak spłaszczyć listę

flat_list = [item for sublist in l for item in sublist]
Talented Thrush

Lista płaska Python z listy

flat_list = [item for sublist in l for item in sublist]

#which is equivalent to this 
flat_list = []
for sublist in l:
    for item in sublist:
        flat_list.append(item)
Batman

spłaszcz listę list Python

flattened = [val for sublist in list_of_lists for val in sublist]
Disturbed Dogfish

spłaszcz listę listy Python

# idiomatic python

# using itertools
import itertools

list_of_list = [[1, 2, 3], [4, 5], [6]]
chain = itertools.chain(*images)

flattened_list = list(chain)
# [1, 2, 3, 4, 5, 6]
Adventurous Alligator

Lista Python Flatten

itertools.chain.from_iterable(factors)
Blushing Butterfly

Python spłaszcza listę list

from collections.abc import Iterable

def flatten(l):
    for el in l:
        if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
            yield from flatten(el)
        else:
            yield el
Strange Stag

Odpowiedzi podobne do “Python spłaszcza listę list”

Pytania podobne do “Python spłaszcza listę list”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu