Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2890 connectés 

  FORUM HardWare.fr
  Programmation
  Python

  Chaînes de caractères en Python

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Chaînes de caractères en Python

n°2393011
jpl38
Posté le 10-08-2021 à 10:06:46  profilanswer
 

Bonjour
Ma question porte sur les chaînes en Python.
Si je considère la chaîne chn = "aaa", il me semble qu'elle contient deux fois la sous-chaîne "aa", la première en position 0, et la deuxième en position 1.
Mais l'instruction  

Code :
  1. chn.count('aa')


me renvoie 1.  
Quelqu'un pourrait-il m'éclairer ?


Message édité par jpl38 le 10-08-2021 à 10:07:39
mood
Publicité
Posté le 10-08-2021 à 10:06:46  profilanswer
 

n°2393013
DDT
Few understand
Posté le 10-08-2021 à 10:30:33  profilanswer
 

https://docs.python.org/3/library/stdtypes.html
 

Citation :

str.count(sub[, start[, end]])
 
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.


---------------
click clack clunka thunk
n°2393023
jpl38
Posté le 10-08-2021 à 12:30:36  profilanswer
 

DDT a écrit :

https://docs.python.org/3/library/stdtypes.html
 

Citation :

str.count(sub[, start[, end]])
 
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.



Ok, merci. J'aurais dû prendre la peine d'aller voir la doc ...
Donc count ne sert qu'à dénombrer les divers caractères d'une chaîne.
Ça me semble un peu inabouti.

n°2393306
Feitan21
Posté le 13-08-2021 à 19:44:52  profilanswer
 

Sinon en passant par du regex tu peux avoir les iterations et leur position assez facilement :  
 

Code :
  1. matches = re.finditer("(?=(aa))", "aaa" )


Puis tu itères sur les matches :  
 

Code :
  1. [(match.group(1), match.start()) for match in matches]


Résultat : [('aa', 0), ('aa', 1)]

Message cité 1 fois
Message édité par Feitan21 le 13-08-2021 à 19:45:14
n°2393320
jpl38
Posté le 14-08-2021 à 11:40:25  profilanswer
 

Feitan21 a écrit :

Sinon en passant par du regex tu peux avoir les iterations et leur position assez facilement :  
 

Code :
  1. matches = re.finditer("(?=(aa))", "aaa" )


Puis tu itères sur les matches :  
 

Code :
  1. [(match.group(1), match.start()) for match in matches]


Résultat : [('aa', 0), ('aa', 1)]


Merci, je vais creuser cette voie


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

  Chaînes de caractères en Python

 

Sujets relatifs
[PYTHON] revenir "menu principal"Création d'une clé de registre dans un paquet python
[PYTHON] récupérer données[Python] Itération sur un DataFrame (pandas)
[python] Youtube comment downloader[Python] - Utilisation dictionnaire
Python et PillowXML espace dans les noms de noeud, acces en python
Mini projet python (Maintenance machines) 
Plus de sujets relatifs à : Chaînes de caractères en Python


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR