[英]List slicing issues in python
This code seems like it should work. 此代码似乎应该工作。 It sums up the number of words that are "striped" (letter-consonant-letter-etc.) and then returns the sum.
它求和“带状”(letter-consonant-letter-等)的单词总数,然后返回总和。 However when I test it with
print (striped("My name is ...") )
it only counts my
and is
and gives me a sum of 2 instead of 3... why is name
missing? 但是,当我用
print (striped("My name is ...") )
测试它时,它仅计算my
和is
并且给我2而不是3的总和...为什么缺少name
?
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
def striped(text):
my_sum = 0
text = text.replace(".", " ").replace(",", " ").split()
vowels = VOWELS.lower()
consonants = CONSONANTS.lower()
for word in text:
word = word.lower()
if ((word[::2] in vowels and word[1::2] in consonants)\
or (word[::2] in consonants and word[1::2] in vowels))\
and len(word) > 1:
print (word)
my_sum += 1
return my_sum
You should use set.issubset()
instead. 您应该改用
set.issubset()
。
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
def striped(text):
my_sum = 0
text = text.replace(".", " ").replace(",", " ").split()
vowels = set(c for c in VOWELS.lower())
consonants = set(c for c in CONSONANTS.lower())
for word in text:
word = word.lower()
if ((set(word[::2]).issubset(vowels) and set(word[1::2]).issubset(consonants))\
or (set(word[::2]).issubset(consonants) and set(word[1::2]).issubset(vowels)))\
and len(word) > 1:
print (word)
my_sum += 1
return my_sum
striped('My name is...')
The reason it works for my
and is
is that they are two char words, so you are checking if if m
is in the string of constants, and if y
is in the string of vowels, which works. 它工作的原因
my
并is
是,他们是两个字符的话,那么你正在检查,如果如果m
是常量字符串中,如果y
是元音的字符串,其工作的地方。 For longer words like name
, then clearly nm
is not in the string of sonsonants, so it fails. 对于更长的单词,如
name
,则显然nm
不在子音串中,因此它失败了。
Instead, you should use sets. 相反,您应该使用集合。 Essentially, you want to find if
set(['n','m'])
is a subset of the set of consonants. 本质上,您想确定
set(['n','m'])
是否是辅音集的子集。
Here is a solution with lists. 这是带有列表的解决方案。 The problem with your code is that words longer than two characters return a substring when you use
[::2]
rather than single characters that are tested whether they are contained in vowels
/ constants
. 您的代码的问题在于,当您使用
[::2]
时,长度超过两个字符的单词会返回一个子字符串,而不是用来测试它们是否包含在vowels
/ constants
单个字符。 By converting it to a list first, you can check every item of the list whether it is contained in the according set of characters. 通过首先将其转换为列表,可以检查列表中的每个项目是否包含在相应的字符集中。
VOWELS = "AEIOUY"
CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ"
def striped(text):
my_sum = 0
text = text.replace(".", " ").replace(",", " ").split()
vowels = VOWELS.lower()
consonants = CONSONANTS.lower()
for word in text:
word = word.lower()
if ((all(c in vowels for c in list(word[::2]))\
and all(c in consonants for c in list(word[1::2])))\
or (all(c in consonants for c in list(word[::2]))\
and all(c in vowels for c in list(word[1::2]))))\
and len(word) > 1:
print (word)
my_sum += 1
return my_sum
print striped("My name is")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.