[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] python, "pakke liste ud"



Atte André Jensen skrev:

Hejsa

Jeg har en liste fx

l = [1,[2,3],4, [5,6]]

og vil gerne have det lavet om til:

l = [[1,2,4,5], [1,2,4,6], [1,3,4,5], [1,3,4,6]]

Dvs en liste med en liste for hver kombination af elementer fra sub-lister. Jeg kunne selvfølgelig kode det selv, men jeg har på fornemmelsen at det enten helt eller delvist allerede findes i python.

Desværre kan jeg ikke nogle gode ord for ovenståede at hælde i google, så jeg håber én her kan sige om det findes og i givet fald hvor...

Hej,

jeg kan desværre ikke sige, om der findes noget indbygget i Python. Hvis der var tale om at finde kombinationer fra et veldefineret antal lister, ville "list comprehension" vist være mest oplagt, men når det er en liste af lister, bliver det jo lidt mere kompliceret.

I dit eksempel består listen af både lister og heltal, Hvis du konverterer den til kun lister, altså:

l = [[1],[2,3],[4], [5,6]]

vil jeg mene at følgende giver det ønskede resultat:

def (lsts):
 kombinationer = [[x] for x in lsts[0]]
return reduce(lambda lst1,lst2:[x+[y] for x in lst1 for y in lst2], lsts[1:], kombinationer)

Dermed selvfølgelig ikke sagt det er den bedste løsning.

mvh
Jakob



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2008-03-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *