Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列出python中的切片问题_Python_Python 3.x_Slice - Fatal编程技术网

列出python中的切片问题

列出python中的切片问题,python,python-3.x,slice,Python,Python 3.x,Slice,这段代码似乎应该可以工作。它将“条带化”(字母-辅音-字母-等)的单词数相加,然后返回总和。但是,当我使用print(条纹(“我的名字是…)测试它时,它只计算My和is,并给我一个2而不是3的总和。。。为什么缺少名称 VOWELS = "AEIOUY" CONSONANTS = "BCDFGHJKLMNPQRSTVWXZ" def striped(text): my_sum = 0 text = text.replace(".", " ").replace(",", " "

这段代码似乎应该可以工作。它将“条带化”(字母-辅音-字母-等)的单词数相加,然后返回总和。但是,当我使用
print(条纹(“我的名字是…)
测试它时,它只计算
My
is
,并给我一个2而不是3的总和。。。为什么缺少
名称

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        

您应该改用
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...')
它对
my
有效的原因是它们是两个字符词,因此您要检查
m
是否在常量字符串中,以及
y
是否在元音字符串中,这是有效的。对于较长的单词,如
name
,则显然
nm
不在sonants字符串中,因此失败



相反,您应该使用集合。本质上,您希望找到
set(['n','m'])
是否是辅音集的子集

这里有一个列表解决方案。代码的问题是,当您使用
[::2]
时,超过两个字符的单词返回一个子字符串,而不是测试它们是否包含在
元音中的单个字符。
通过首先将其转换为列表,可以检查列表中的每个项目是否包含在相应的字符集中

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")

也不要使用
sum
!阴影的建筑,现在也改变了!仍然不工作。。。我一直用sum来做这个,到目前为止,它没有给我带来任何问题…?你应该修正缩进。再次剪切和粘贴错误。。。固定的now@ejLev哦,对不起,我只是指出,如果你有一套东西,你可以生成互补集,而不是硬编码它。换句话说,如果你有一组元音,那么辅音集就是互补集(在大写ascii字母中)。因此您可以生成它,而不是硬编码它。当集合较大时,该值更大。:)
set
的问题是,每个字母只有一次,因此对于某些字符出现多次的单词,它不起作用。您可以改为使用
list
。但这不起作用,因为现在我只是用striped(“ballllllll”)尝试了它,它给了我一个
my_sum
值1,当它显然应该是0时,您应该在原始问题中包含一组测试值@ejLev@EspartaPalma你这是什么意思?在你的帖子上加上要测试的值:striped(“我的名字是…”-->3条带(“balllllllll”)-->0….这里需要一个额外的括号,将
语句与
分隔开来。谢谢。这工作做得很好,但是我仍然不能完全理解整个
(all(c在元音中表示列表中的c)(单词[::2])
你有没有可能一部分一部分地简单解释一下呢?当然,让我们先从
列表(word[::2])
开始。假设单词是“name”,那么
[::2]
返回的是“nm”,它不会是元音,因为整个字符串都经过测试,而不是单个字符。这就是为什么它会首先转换为列表的原因。
对于列表中的c(word[::2])
这意味着对单词的每个字符(
c
)进行迭代。如果iterable的所有元素均为true,则单独检查每个字符是否包含在
元音中。在本例中,元音中的
c
与in-line for循环结合使用,则返回true。另请参阅。