“Daftar Flatten” Kode Jawaban

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

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 perataan Python

itertools.chain.from_iterable(factors)
Blushing Butterfly

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

Daftar Flatten

regular_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9]]
flat_list = [item for sublist in regular_list for item in sublist]
print('Original list', regular_list)
print('Transformed list', flat_list)
Clever Caterpillar

Daftar Flatten

regular_list = [[1, 2, 3, 4], [5, 6, 7], [8, 9]]
flat_list = [item for sublist in regular_list for item in sublist]
print('Original list', regular_list)
print('Transformed list', flat_list)
Clever Caterpillar

Jawaban yang mirip dengan “Daftar Flatten”

Pertanyaan yang mirip dengan “Daftar Flatten”

Lebih banyak jawaban terkait untuk “Daftar Flatten” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya