查找数组是否包含有一个不匹配的字符串

发布于 2025-01-12 16:54:25 字数 726 浏览 0 评论 0原文

我想说列表中有一个字符串只有一个不匹配

def check(list, s):
    n = len(list)
 
    # If the array is empty
    if (n == 0):
        return False
 
    for i in range(0, n, 1):
         
        # If sizes are same
        if (len(list[i]) != len(s)):
            continue
 
        diff = False
        for j in range(0, len(list[i]), 1):
            if (list[i][j] != s[j]):
 
                # If first mismatch
                if (diff == False):
                    diff = True
 
                # Second mismatch
                else:
                    diff = False
                    break
                 
        if (diff):
            return True
 
    return False

这段代码没问题,但运行速度很慢。如何使用字典使其更快?

I want say there is a string in the list with only one mismatch

def check(list, s):
    n = len(list)
 
    # If the array is empty
    if (n == 0):
        return False
 
    for i in range(0, n, 1):
         
        # If sizes are same
        if (len(list[i]) != len(s)):
            continue
 
        diff = False
        for j in range(0, len(list[i]), 1):
            if (list[i][j] != s[j]):
 
                # If first mismatch
                if (diff == False):
                    diff = True
 
                # Second mismatch
                else:
                    diff = False
                    break
                 
        if (diff):
            return True
 
    return False

This code is okay but it works slowly. How can I make it faster using a dictionary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2025-01-19 16:54:25

您可以使用 zip() 将两个字符串的每个字符配对,并使用 sum() 来计算不匹配的数量。 any() 函数将允许您检查列表中每个项目的匹配:

def check(sList,string):
    return any(len(string)==len(s) and sum(a!=b for a,b in zip(s,string))==1 
               for s in sList)

check(["abcd","cdef","efgh"],"xfgh") # True
check(["abcd","cdef","efgh"],"abdc") # False

这将转换为以下基本 for 循环:

def check(sList,string):
    for s in sList:
        if len(string) !=len(s): continue
        if sum(a!=b for a,b in zip(s,string)) == 1:
            return True
    return False

[编辑] 为了使其运行得更快一点,字符串比较可以使用 zip() 作为一个迭代器,以便比较不需要转到每个字符串的末尾:

def check(sList,string):
    for s in sList:
        if len(string) !=len(s): continue
        mismatch = (a!=b for a,b in zip(s,string))  # non-matching iterator
        if not any(mismatch): continue              # at least one
        if any(mismatch): continue                  # no more after that
        return True
    return False

You can use zip() to pair up each character of two strings and sum() to count the number of mismatches. The any() function will allow you to check for a match over every item in the list:

def check(sList,string):
    return any(len(string)==len(s) and sum(a!=b for a,b in zip(s,string))==1 
               for s in sList)

check(["abcd","cdef","efgh"],"xfgh") # True
check(["abcd","cdef","efgh"],"abdc") # False

This translates to the following basic for-loop:

def check(sList,string):
    for s in sList:
        if len(string) !=len(s): continue
        if sum(a!=b for a,b in zip(s,string)) == 1:
            return True
    return False

[EDIT] to make this go a bit faster, the string comparisons can use zip() as an iterator so that the comparison doesn't need to go to the end of every string:

def check(sList,string):
    for s in sList:
        if len(string) !=len(s): continue
        mismatch = (a!=b for a,b in zip(s,string))  # non-matching iterator
        if not any(mismatch): continue              # at least one
        if any(mismatch): continue                  # no more after that
        return True
    return False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文