简体   繁体   English

Python:如果字符串 1 包含在字符串 2 中,如何检查两个具有重复字母的字符串?

[英]Python: How can I check between two strings that have repeating letters if string 1 is contained within string 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.在上面的代码中:我试图找出如果 letter_word 包含 target_word 时如何返回 True。

So 'ell' in 'helo' would return False But 'ell' in 'hello' would return True所以 'helo' 中的 'ell' 会返回 False 但 'hello' 中的 'ell' 会返回 True

Try to use in :尝试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

You can use in but you first have to make the cases the same:您可以使用in但首先必须使大小写相同:

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

You're doing a case-insensitive search.您正在进行不区分大小写的搜索。 No need to do a list comprehension.无需进行列表理解。 Just use lower() or upper() to convert all characters to lower or uppercase and use in :只需使用lower()upper()将所有字符转换为小写或大写并in使用:

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

Alternatively you could do a case insensitive regex search:或者,您可以进行不区分大小写的正则表达式搜索:

import re

def can_spell_with(target_word, letter_word):
    return re.search(target_word, letter_word, re.IGNORECASE) is not None
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

this will check the whether word1 in word2 irrespective of lowercase or uppercase这将检查 word2 中的 word1 是否为小写或大写

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中找到两个字符串之间的字符串并打印出来? - How can I find and print a string between two strings in python? 如何检查一个字符串是否包含两个或更多字母? - How can I check a string for two letters or more? 检查字符串的所有字母是否都包含在Python的字母列表中 - Check if all letters of string are contained in list of letters in Python 如何使用isalpha检查字符串是否只是python中的字母? - How can I check if a string is only letters in python using isalpha? 如何用不同的值替换dataframe列中的重复字符串(重复的字符串应具有相同的新值) - How can I replace repeating string in dataframe column with a different value (repeating strings should have the same new value) 如何删除包含在同一字符串列表中的其他字符串中的字符串? - How can I drop strings contained in other string contained in the same string list? 如何仅对还包含数字的字符串中的字母大写? - How to uppercase only the letters within a string that also contained numbers? 两个字符串,并让输入的字母打印另一个字符串中的内容 - Two strings, and have the letters inputed printed what is held in the other string 如何检查字符串是否包含python中的所有字母? - How do I check if a string contains ALL letters of the alphabet in python? 如何检查字符串中的 3 个字母是否等于 Python 中的“0-0”? - How do I check if 3 letters in a string are equal to '0-0' in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM