1
votes

Python: Comment puis-je vérifier entre deux chaînes contenant des lettres répétitives si la chaîne 1 est contenue dans la chaîne 2?

def can_spell_with(target_word, letter_word):
    valid = True
    target_word1 = [x.lower() for x in target_word]
    letter_word1 = [x.lower() for x in letter_word]
    for c in target_word1:
        if c not in letter_word1:
            valid = False
        else:
            valid = True
    return valid
print(can_spell_with('elL','HEllo'))
# True
print(can_spell_with('ell','helo'))
# False
In the code above: I am trying to figure out how to return True if the letter_word contains the target_word.So 'ell' in 'helo' would return False
But 'ell' in  'hello' would return True

3 commentaires

la casse des caractères est-elle importante, les majuscules ou les minuscules?


Est-ce que cela répond à votre question? Python a-t-il une méthode de sous-chaîne "contient"?


la capitalisation n'a pas d'importance et merci pour la suggestion, il y a pas mal de détails dans les explications là-bas merci


5 Réponses :


2
votes

Essayez d'utiliser in :

def can_spell_with(a, b):
     return (a.upper() in b.upper())
print(can_spell_with('Ell','HEllo'))
>>>True
print(can_spell_with('ell','helo'))
>>>False


0 commentaires

2
votes

Vous pouvez utiliser dans mais vous devez d'abord rendre les cas identiques:

def can_spell_with(target_word, letter_word):
    return target_word.lower() in letter_word.lower()


4 commentaires

Cela ne fonctionne pas avec le premier exemple. Lorsque vous faites print (can_spell_with ('Ell', 'HEllo')) , la sortie est False , quand elle est vraie.


Devrait être "can_spell_with (letter_word, target_word)" Cela fait "b in a" par opposition à "a in b"


Ouais @Michael, c'est ça le problème


Oh désolé, je vais réparer ça



2
votes

Vous effectuez une recherche insensible à la casse. Pas besoin de faire une compréhension de liste. Utilisez simplement lower () ou upper () pour convertir tous les caractères en minuscules ou majuscules et utilisez dans :

import re

def can_spell_with(target_word, letter_word):
    return re.search(target_word, letter_word, re.IGNORECASE) is not None

Vous pouvez également effectuer une recherche d'expression régulière insensible à la casse:

def can_spell_with(target_word, letter_word):
    return target_word.lower() in letter_word.lower()


0 commentaires

1
votes
target_word = target_word.lower() 
letter_word = letter_word.lower()

lenn = len(target_word)     
valid = False               

for i in range(len(letter_word)-lenn):   
    if letter_word[i:i+lenn] == target_word:   
        valid = True
return valid

1 commentaires

Comment fonctionne cette partie: for i in range (len (letter_word) -lenn): if letter_word [i: i + lenn] == target_word: comment sait-il pour 'ell' et 'helo' que le second 'l' n'est-ce pas là dans «helo»? pour moi, il semble qu'il recherche e dans helo puis l dans helo puis l dans helo, ce qui s'avère être vrai



1
votes

ceci vérifiera si mot1 dans mot2 indépendamment des minuscules ou des majuscules

def func(word1, word2):
    first_char = word1[0]
    for index, char in enumerate(word2):
        if char==first_char:
            if word2[index:len(word1)+1]==word1:
                return True
    return False


0 commentaires