Cristian Re ha scritto: > Ciao a tutti, > > volevo sapere se esiste qualche comando python per confrontare due > stringhe e che torni ad esempio quanti caratteri uguali ci sono nella > medesima posizione nelle due stringhe. > > Ad esempio se str1='ciao' e str2='cibo' un comando che torni 3 > > So che sarebbe fattibile confrontando ogni singolo carattere ma mi > chiedevo se tra le utility di python già non ci fosse qualcosa che > gestisce questo.
Un singolo comando no, ma utilizzando set() è abbastanza semplice: >>> str1 = 'ciao' >>> str2 = 'cibo' >>> set1 = set(str1) >>> set2 = set(str2) >>> set1 & set2 set(['i', 'c', 'o']) >>> len(set1 & set2) 3 Oppure in una riga: >>> len(set('ciao') & set('cibo')) 3 Ciao! Simone Chiacchiera con i tuoi amici in tempo reale! http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python