Python 使用直方图功能检查列表中的重复字母
我正试图完成我的家庭作业,但在合并所需的直方图函数时遇到了困难 这是我必须使用的代码:Python 使用直方图功能检查列表中的重复字母,python,list,dictionary,histogram,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"]
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()
的任何实现。相反,您的实现应该使用直方图中的计数来确定是否存在任何重复项
在提供的test\u dups
列表中的字符串上写一个循环。打印列表中的每个字符串,并根据该字符串的返回值has_duplicates()
,确定该字符串是否有重复项。例如,aaa
和abc
的输出如下
"abcdefghijklmnopqrstuvwxyz uses all the letters"
aaa有重复项
abc没有复制品
为test_dup中的每个字符串打印一行,如上面所述
编写一个名为missing_letters
的函数,该函数接受一个字符串参数并返回一个新字符串,其中包含不在参数字符串中的所有字母。返回字符串中的字母应按字母顺序排列
我的实现应该使用histogram()
函数中的直方图。它还应该使用全局变量alphabet
。它应该直接使用这个全局变量,而不是通过参数或本地副本。它应该在字母表中的字母上循环,以确定输入参数中缺少哪些字母
函数missing_letters
应该将缺少的字母列表组合成一个字符串并返回该字符串
在列表test_miss中的字符串上写一个循环,并用每个字符串调用missing_字母
。为每个字符串打印一行,列出缺少的字母。例如,对于字符串“aaa”,输出应如下所示
"abcdefghijklmnopqrstuvwxyz uses all the letters"
aaa缺少字母bcdefghijklmnopqrstuvxyz
如果字符串包含字母表中的所有字母,则输出应表示它使用了所有字母。例如,字符串字母表
本身的输出如下所示
"abcdefghijklmnopqrstuvwxyz uses all the letters"
为test_miss中的每个字符串打印一行,如上面所述
这就是我所能得到的
def has_duplicates(t):
if histogram(t) > 1:
return True
else:
return False
结果:
“str”和“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.
以下应提供所需的结果:
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.
我意识到我确实对这里的一般概念缺乏理解,但如果有人能提供一些指导,我会非常感激。不要求您为我完成作业,因为我真的很想学习如何将直方图值转换为int,以便has_duplicates函数成功执行?您是否测试了他们首先提供给您的代码以查看其返回的内容?我不确定您是如何得到该错误的。你发布的直方图函数返回一个dict
IMO你需要你的问题,并缩小你的提问范围,因为从现在的情况看,你似乎需要整个作业的代码。你的直方图返回一个字典。您必须对这些值进行循环,并检查是否有大于1的值。(例如,def有多个重复项(t):返回任意项(k的v>1,直方图(t)中的v)。items()我意识到我确实对这里的一般概念缺乏理解,但如果有人能提供一些指导,我会非常感激。我不想让你帮我完成作业,因为我真的很想学习如何,但如何开始将直方图值转换为int,以便has_duplicates函数成功执行ssfully?你是否测试过他们首先给你的代码,看看它返回了什么?我不确定你是如何得到这个错误的。你发布的直方图函数返回一个dict
IMO你需要你的问题,并缩小你问的范围,因为从目前的情况看,你似乎需要整个作业的代码。你的直方图URN是一个字典。您必须循环检查这些值,并检查是否有大于1的值。(例如,def有重复项(t):返回任何值(对于k,v>1,在直方图(t)中为v)。items()