listesine katılmak için?Python liste anlama Verilen listelerin
combinedLists = ['hello', 'world', 'foo', 'bar']
listesine katılmak için?Python liste anlama Verilen listelerin
combinedLists = ['hello', 'world', 'foo', 'bar']
lists = [['hello'], ['world', 'foo', 'bar']]
combined = [item for sublist in lists for item in sublist]
Veya:
import itertools
lists = [['hello'], ['world', 'foo', 'bar']]
combined = list(itertools.chain.from_iterable(lists))
İlk tercih benim görüşüme göre daha güzel görünüyor. Itertools kullanmak daha hızlıdır. Bu cevap harika. –
from itertools import chain
combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]
Ben iç içe döngüler kullanarak bunu uzun bir yol yapabiliriz biliyorum ama yapacak bir tek satırlık var acaba aynı şey. – congusbongus