[PIPython] ricerca di una sottostringa

tiziano a work tiziano
Ven 19 Nov 2004 15:16:51 CET


ah, scusa, manca la soluzione ;-)

puoi utilizzare il metodo index di stringa, eventualmente in costrutto try.

try:
  if stringa.index(sottostringa):
    return True
except:
  return False

ciccio a unical.it wrote:

>Salve ragazzi, 
>ho un semplice script per ricercare una sottostringa del tipo: 
> 
>lista1=[["1\t2\t0.000\t2.2322\t0.001\n"],["1\t3\t0.000\t1.1123\t0.201\n"]] 
>lista2=["1\t3\t0.000\t1.1123\n"] 
>for i in lista1: 
>     if lista2[0].strip() in i[0]: 
>             print i[0].strip()+"\t"+((lista2[0].strip()).split("\t"))[3]+"\n" 
>     else: 
>             print i[0] 
> 
>Se lo faccio correre su python 2.3 funziona e restituisce: 
> 
>1       2       0.000   2.2322  0.001 
> 
>1       3       0.000   1.1123  0.201   1.1123 
> 
>Mentre se lo faccio correre su python 2.2 ottengo il seguente errore: 
> 
>Traceback (most recent call last): 
>  File "p.py", line 4, in ? 
>    if lista2[0].strip() in i[0]: 
>TypeError: 'in <string>' requires character as left operand 
> 
>e non so' a cosa si riferisce. Poiche' posso lavorare solo su python 2.2 e non 
>ho la possibilita' di aggiornare il sistema, potreste suggerirmi come ovviare 
>a questo errore? 
> 
>grazie 
> 
>ernesto 
> 
> 
> 
>
>-------------------------------------------------
>This mail sent through IMP: http://horde.org/imp/
>
>_______________________________________________
>Python mailing list
>Python a lists.python.it
>http://lists.zope.it/cgi-bin/mailman/listinfo/python
>
>  
>




More information about the Python mailing list