如果代码检查所有情况,我该如何制作?
How can I make if code check all cases?
#Checking if a word is an isogram
from collections import Counter
def count_isogram(words, index):
a=Counter(words[int(index)])
d=words[int(index)]
for (b,c) in a.items():
if c >= 2:
print(b,c)
return(d+' is not an isogram')
else:
print(b,c)
return(d+' is an isogram')
你好,上面是我的代码。我正在尝试制作一个非常基本的等值图检查器(等值图是一个没有任何重复字母的词(狗、猫、鸟等))。我的代码大部分都在工作,但是当我到达我的if 语句,它检查每个单词的第一个字母以确定使用哪个 return 短语。如何让我的代码检查每个字母?例如下图 link 演示了问题(我不post 图片还没有足够高的代表):
示例:
您可以看到第一个场景中的单词:'silly'(1 的索引)正在通过函数 运行 但是因为只有 1 个 S,它 return是说这个词是等值线图,而实际上不是。 运行宁词 'dude'(索引为 1)时,因为第一个字母在该词中出现不止一次,所以 运行 是正确的 return,但它只是因为检查的第一个字母是重复的。
我尝试了 运行ning c.all()
、c.any()
和其他一些运算符,但它不起作用,因为 c 是一个只有 1 个值的整数。
我怎样才能 change/add 使代码在 运行 宁 return 之前检查所有可能的字母?
您的 if
和 else
return,这意味着您将始终 return 在循环的第一次迭代中。相反,如果发现单词不是等值线图,循环应该只 return 。只有当循环终止而没有得到这样的结论时,你才应该 return 这个词是一个等值图:
for (b,c) in a.items():
if c >= 2:
return(d+' is not an isogram')
return(d+' is an isogram')
解决方案
您的问题的解决方案是 return 您的程序的最终结果,在您完成对每个字母的循环之后。
对于每个字母,检查它的计数是否大于或等于 2
。如果是,return 立即显示适当的消息 (return word + ' is not an isogram'
)。但是,如果您到达循环的末尾,您就会知道该词确实是一个等值线图,因此您可以 return 其他消息 (word + ' is an isogram'
)
from collections import Counter
def is_isogram(words, index):
el = words[index]
letter_occurences = Counter(el)
for word, count in letter_occurences.items():
print(word, count)
if count >= 2:
return word + ' is not an isogram'
return word + ' is an isogram'
算法改进
因为我们知道等值线图是
any word that has no repeating letters
我们可以使用 set
从字符串中删除任何可能的重复字母。然后我们可以将集合的长度与字符串的原始长度进行比较。如果长度相等,则没有字母被删除并且该词是一个等值线图。如果它们不相等,我们就知道这个词不是一个等值线:
def is_isogram(words, index):
word = words[index]
if len(set(word)) == len(word):
return word + ' is an isogram'
return word + ' is not an isogram'
这比原来的方法快很多(准确地说大约快九倍):
------------------------------------
| Original method | 6.47415304184 |
------------------------------------
| Above method | 0.669512987137 |
------------------------------------
#Checking if a word is an isogram
from collections import Counter
def count_isogram(words, index):
a=Counter(words[int(index)])
d=words[int(index)]
for (b,c) in a.items():
if c >= 2:
print(b,c)
return(d+' is not an isogram')
else:
print(b,c)
return(d+' is an isogram')
你好,上面是我的代码。我正在尝试制作一个非常基本的等值图检查器(等值图是一个没有任何重复字母的词(狗、猫、鸟等))。我的代码大部分都在工作,但是当我到达我的if 语句,它检查每个单词的第一个字母以确定使用哪个 return 短语。如何让我的代码检查每个字母?例如下图 link 演示了问题(我不post 图片还没有足够高的代表):
示例:
您可以看到第一个场景中的单词:'silly'(1 的索引)正在通过函数 运行 但是因为只有 1 个 S,它 return是说这个词是等值线图,而实际上不是。 运行宁词 'dude'(索引为 1)时,因为第一个字母在该词中出现不止一次,所以 运行 是正确的 return,但它只是因为检查的第一个字母是重复的。
我尝试了 运行ning c.all()
、c.any()
和其他一些运算符,但它不起作用,因为 c 是一个只有 1 个值的整数。
我怎样才能 change/add 使代码在 运行 宁 return 之前检查所有可能的字母?
您的 if
和 else
return,这意味着您将始终 return 在循环的第一次迭代中。相反,如果发现单词不是等值线图,循环应该只 return 。只有当循环终止而没有得到这样的结论时,你才应该 return 这个词是一个等值图:
for (b,c) in a.items():
if c >= 2:
return(d+' is not an isogram')
return(d+' is an isogram')
解决方案
您的问题的解决方案是 return 您的程序的最终结果,在您完成对每个字母的循环之后。
对于每个字母,检查它的计数是否大于或等于 2
。如果是,return 立即显示适当的消息 (return word + ' is not an isogram'
)。但是,如果您到达循环的末尾,您就会知道该词确实是一个等值线图,因此您可以 return 其他消息 (word + ' is an isogram'
)
from collections import Counter
def is_isogram(words, index):
el = words[index]
letter_occurences = Counter(el)
for word, count in letter_occurences.items():
print(word, count)
if count >= 2:
return word + ' is not an isogram'
return word + ' is an isogram'
算法改进
因为我们知道等值线图是
any word that has no repeating letters
我们可以使用 set
从字符串中删除任何可能的重复字母。然后我们可以将集合的长度与字符串的原始长度进行比较。如果长度相等,则没有字母被删除并且该词是一个等值线图。如果它们不相等,我们就知道这个词不是一个等值线:
def is_isogram(words, index):
word = words[index]
if len(set(word)) == len(word):
return word + ' is an isogram'
return word + ' is not an isogram'
这比原来的方法快很多(准确地说大约快九倍):
------------------------------------
| Original method | 6.47415304184 |
------------------------------------
| Above method | 0.669512987137 |
------------------------------------