“Daftar Python Flatten” Kode Jawaban

Python cara meratakan daftar

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

Daftar perataan Python

l = [[1, 2], [3, 4], [5, 6, 7]]
flat_list = [item for sublist in l for item in sublist]
# flat_list = [1, 2, 3, 4, 5, 6, 7]
Wandering Willet

Daftar datar Python dari daftar daftar

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

Ratakan daftar Python

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

Ratakan daftar Python daftar

# 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

Daftar Python Flatten

nested_lists = [[1, 2], [[3, 4], [5, 6], [[7, 8], [9, 10], [[11, [12, 13]]]]]]
flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
flatten(nested_lists)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
patrick204nqh

Jawaban yang mirip dengan “Daftar Python Flatten”

Pertanyaan yang mirip dengan “Daftar Python Flatten”

Lebih banyak jawaban terkait untuk “Daftar Python Flatten” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya