python - 使用直方图函数检查列表中的重复字母

标签 python list dictionary histogram

我正在努力完成我的作业,但在合并所需的直方图函数时遇到困难。

这是我必须使用的代码:

alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

def histogram(s):
     d = dict()
     for c in s:
          if c not in d:
               d[c] = 1
          else:
               d[c] += 1
     return d

我需要编写一个名为 has_duplicates() 的函数,该函数接受一个字符串参数,如果字符串有任何重复字符,则返回 True。否则,它应该返回False

通过使用上面的 histogram() 函数创建直方图来实现 has_duplicates()。不要使用教科书中给出的任何 has_duplicates() 实现。相反,您的实现应该使用直方图中的计数来确定是否存在重复项。

在提供的 test_dups 列表中的字符串上编写一个循环。根据该字符串的 has_duplicates() 返回值打印列表中的每个字符串以及它是否有重复项。例如,aaaabc 的输出如下。

aaa has duplicates
abc has no duplicates

为 test_dups 中的每个字符串打印类似于上述之一的行。

编写一个名为 missing_letters 的函数,它接受一个字符串参数并返回一个新字符串,其中包含参数字符串中不存在的所有字母。返回字符串中的字母应按字母顺序排列。

我的实现应该使用 histogram() 函数中的直方图。它还应该使用全局变量alphabet。它应该直接使用这个全局变量,而不是通过参数或本地副本。它应该循环遍历 alphabet 中的字母,以确定输入参数中缺少哪些字母。

函数 missing_letters 应该将缺失字母的列表组合成一个字符串并返回该字符串。

对列表 test_miss 中的字符串编写一个循环,并对每个字符串调用 missing_letters。为每个字符串打印一行,列出缺失的字母。例如,对于字符串“aaa”,输出应如下所示。

aaa 缺少字母 bcdefghijklmnopqrstuvwxyz

如果字符串包含 alphabet 中的所有字母,则输出应显示它使用了所有字母。例如,字符串 alphabet 本身的输出如下。

"abcdefghijklmnopqrstuvwxyz uses all the letters"

为 test_miss 中的每个字符串打印类似于上述之一的行。

这是我所知道的...

def has_duplicates(t):
    if histogram(t) > 1:
        return True
    else:
        return False

结果:

'>' not supported between instances of 'str' and 'int'

最佳答案

以下内容应提供所需的结果:

alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

def histogram(s):
     d = dict()
     for c in s:
          if c not in d:
               d[c] = 1
          else:
               d[c] += 1
     return d

def has_duplicates(s):
    # Return False if each letter in s is not distinct
    return len(histogram(s)) != len(s)

def missing_letters(s):
    h = histogram(s)
    rv = ''
    # Loop over letters in alphabet, if the letter is not in the histogram then
    # append to the return string.
    for c in alphabet:
        if c not in h:
            rv = rv + c
    return rv

# Loop over test strings as required.
for s in test_miss:
    miss = missing_letters(s)
    if miss:
        print(f"{s} is missing letters {miss}.")
    else:
        print(f"{s} uses all the letters.")

输出:

zzz is missing letters abcdefghijklmnopqrstuvwxy.
subdermatoglyphic is missing letters fjknqvwxz.
the quick brown fox jumps over the lazy dog uses all the letters.

关于python - 使用直方图函数检查列表中的重复字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58514172/

相关文章:

python - 解决 pygame 三角函数错误

python - 按 Pandas 组检查 5 个不同年份的值

python - 如何在 Python 中创建用户定义的列表?

list - 我可以在对Perl 6列表进行分类时修改值吗?

c++ - 通过 C++ 套接字发送 std::map<int, std::map<std::string, double>> (linux)

python - 关于多处理管理器与字典键的混淆

python - 如何对 Enum 的 __init__ 方法进行单元测试?

python - 32 字节数据的最短编码

list - Markdown:列表转换不好

swift - Xcode 如何将按钮添加到 ImageView