“Python -Liste flach” Code-Antworten

Python, wie man eine Liste verflachert

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

Python Flacherliste

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

Python flache Liste aus der Liste der Liste

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

Führen Sie eine Liste von Listen Python ab

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

Flach eine Liste der Listen -Python ab

# 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

Python -Liste flach

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

Ähnliche Antworten wie “Python -Liste flach”

Fragen ähnlich wie “Python -Liste flach”

Weitere verwandte Antworten zu “Python -Liste flach” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen