Python 字符串中的字符匹配

标签 python python-3.x algorithm

编写一个函数repfree(s),它将字符串s作为输入并检查是否有任何字符出现多次。如果没有重复,该函数应返回 True,否则返回 False。

我已经尝试过,但我认为这不是解决问题的有效方法。您能建议一个有效的代码吗,谢谢?

def repfree(s):
    newlist = []
    for i in range(0, len(s)):
        newlist.append(s[i])
    newlist2 = set(newlist)
    if len(newlist) == len(newlist2):
        print("True")
   else:
        print("False")

最佳答案

满足此要求的一个简单方法是使用正则表达式。您可能不被允许使用它们,但如果可以,请考虑以下内容:

def repfree(s):
    if re.search(r'^.*(.).*\1.*$', s):
        print("True")
    else:
        print("False")

关于Python 字符串中的字符匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163935/

相关文章:

python - 当我有自定义身份验证模型时,如何登录 Django Rest 可浏览 API?

python - 让 Vim 了解 python 的 ctag 类型注释

python - Python C API 中的多重继承

python - 编程新手。尝试使用 python 3 将相同的文本添加到列中所有数据条目的末尾

python - Python中如何处理带有参数的异常

c# - 如何对列表进行排序,将 map 从旧位置保存到新位置?

c# - 是否有一种已知的快速算法来查找与给定数字相乘的所有数字对?

python - 在 Python 脚本中的 Windows 中的 C++ 命令行编译

python-3.x - Matplotlib动画报错 "Requested MovieWriter (ffmpeg) not available"

algorithm - 该算法的递归关系是什么?