ciao, così risolvo tutto in un paio di righe, davvero pythonico...
grazie mille, simone Il giorno 22/gen/2010, alle ore 00.57, Marco Beri ha scritto: > 2010/1/22 simone gabbriellini <simone.gabbriell...@gmail.com> > Salve, > una curiosità da neofita. > esiste un modo per scrivere in modo più conciso e performante il seguente > codice? > > a = sum([True for element in lista if sum(element.attribute)==0]) > b = sum([True for element in lista if sum(element.attribute)==1]) > c = sum([True for element in lista if sum(element.attribute)==2]) > d = sum([True for element in lista if sum(element.attribute)==3]) > e = sum([True for element in lista if sum(element.attribute)==4]) > > in questo caso avevo pensato a: > > a=[] > b=[] > c=[] > d=[] > e=[] > for element in lista: > if sum(element.attribute) ==0: > a.append(....) > elif .... > ..... > > in questo modo si riduce il numero di volte che scorro "lista", giusto? > ma è effettivamente il modo corretto di affrontare una struttura logica come > questa? > > Mi piace di piu` cosi`: > > >>> d = dict((i, sum([1 for e in lista if sum(e.attribute) == i])) for i in > >>> range(5)) > > In d hai un dizionario ma se vuoi a tutti i costi i risultati in a, b, c, d, > e dopo puoi: > > >>> a, b, c, d, e = [d.get(i, 0) for i in range(5)] > > Ciao. > Marco. > > -- > http://python.thinkcode.tv - Videocorso di Python > http://stacktrace.it - Aperiodico di resistenza informatica > http://beri.it - Blog di una testina di vitello > > _______________________________________________ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python