Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > fr.comp.lang.python > #3888
| Path | csiph.com!pasdenom.info!news.izac.org!reader |
|---|---|
| From | Benoit Izac <use.reply.to@INVALID.ADDRESS> |
| Newsgroups | fr.comp.lang.python |
| Subject | Re: Comportement étrange avec IN et OR... |
| Date | Thu, 26 May 2022 11:38:45 +0200 |
| Message-ID | <871qwg3a6y.fsf@izac.org> (permalink) |
| References | <t6ndp3$1igp$1@gioia.aioe.org> |
| Reply-To | benoit.izac@free.fr |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Info | keg.izac.org; logging-data="1547642"; mail-complaints-to="usenet@izac.org" |
| Xref | csiph.com fr.comp.lang.python:3888 |
Show key headers only | View raw
Bonjour,
Le 26/05/2022 à 10:29, Dominique a écrit dans le message
<t6ndp3$1igp$1@gioia.aioe.org> :
> Je dois commettre une erreur, mais je ne vois pas laquelle. J'extrais
> une unique ligne d'un fichier log :
>
> test='[22118.286892] i915 0000:00:02.0: [drm] *ERROR* AUX C/DDI C
> (TC)/PHY TC1: did not complete or timeout within 10ms (status
> 0xad4003ff)'
>
> Si je fais :
>
> 'ERR' in test
> Out[122]: True
>
> Le TRUE est valable, car il y a bien le mot ERRROR.
Car la chaîne de caractères contient bien « ERR » plus précisément.
> Pour travailler sur des 'mots', je transforme mon string en une liste :
>
> test2=list(test.split(' '))
>
> On poursuit
>
> test2[4]
> Out[127]: '*ERROR*'
>
> Tout est logique. Ensuite, je comprends moins bien :
>
> 'ERR' in test2
> Out[129]: False
'ERR' n'est pas un éléments de la liste donc c'est normal.
> Or, '*ERROR*' in test2
> Out[130]: True
'*ERROR*' en est un.
> D'accord, dans une liste, Python cherche une occurrence exacte. Si je
> poursuis mes recherches :
>
> '*ERROR*' or 'ERR' in test2
> Out[131]: '*ERROR*'
>
> Il me sort *ERROR* alors que je voulais simplement une sortie
> booléenne, mais peu importe. Je remplace test2[4] avec 'essai' (je
> supprime *ERROR*) et j'ajoute 'ERR' à test2 ;
Ce que tu veux c'est :
'*ERROR*' in test2 or 'ERR' in test2
ou encore :
any(s in test2 for s in ['*ERROR*', 'ERR'])
> test2[4]='essai' puis test2.append('ERR')
> Test2 est correctement modifiée. Or, si je fais ça :
>
> '*ERROR*' or 'ERR' in test2
> Out[137]: '*ERROR*'
>
> Il me retrouve le mot'*ERROR*' que j'ai supprimé et pas 'ERR' que j'ai
> ajouté !
>
> C'est d'autant plus étrange que 'ERRE' est bien trouvé :
>
> 'ERR' in test2
> Out[138]: True
>
> Qu'est-ce qui peut expliquer ce comportement ?
>>> 'foo' or 'bar'
'foo'
>>> 'bar' or 'foo'
'bar'
>>> False or 'foo'
'foo'
>>> False or False
False
Le « or » renvoie la première occurrence qui n'est pas fausse. Une
chaîne de caractère non vide est toujours vrai.
--
Benoit Izac
Back to fr.comp.lang.python | Previous | Next — Previous in thread | Find similar
Comportement étrange avec IN et OR... Dominique <zzz@aol.com.invalid> - 2022-05-26 10:29 +0200
Re: Comportement étrange avec IN et OR... Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-05-26 11:27 +0200
Re: Comportement étrange avec IN et OR... Dominique <zzz@aol.com.invalid> - 2022-05-26 17:26 +0200
Re: Comportement étrange avec IN et OR... Olivier Miakinen <om+news@miakinen.net> - 2022-05-26 17:52 +0200
Re: Comportement étrange avec IN et OR... Dominique <zzz@aol.com.invalid> - 2022-05-26 19:01 +0200
Re: Comportement étrange avec IN et OR... Dominique <zzz@aol.com.invalid> - 2022-05-26 11:36 +0200
Re: Comportement étrange avec IN et OR... Benoit Izac <use.reply.to@INVALID.ADDRESS> - 2022-05-26 11:38 +0200
csiph-web