Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 - Fatal编程技术网

Python 列表超出范围

Python 列表超出范围,python,python-2.7,Python,Python 2.7,我正在尝试根据以下示例进行信件排序: 输入: people enjoy programming 输出: [(set(['e', 'o']), set(['l']), set(['p'])), (set(['e', 'o']), set(['j']), set(['n', 'y'])), (set(['o', 'a', 'i']), set(['g', 'm']), set(['p', 'r', 'n']))] [[set(['e', 'o']), set(['l']), set(['p'

我正在尝试根据以下示例进行信件排序:

输入:

people enjoy programming
输出:

[(set(['e', 'o']), set(['l']), set(['p'])), 
(set(['e', 'o']), set(['j']), set(['n', 'y'])), 
(set(['o', 'a', 'i']), set(['g', 'm']), set(['p', 'r', 'n']))]
[[set(['e', 'o']), set(['l']), set(['p'])], [set(['e', 'o']), set(['j']), set(['y', 'n'])], [set(['a', 'i', 'o']), set(['m', 'g']), set(['p', 'r', 'n'])]]
[(set(['e', 'o']), set(['l']), set(['p'])), (set(['e', 'o']), set(['j']), set(['y', 'n'])), (set(['a', 'i', 'o']), set(['m', 'g']), set(['p', 'r', 'n']))]
这是我的代码:

lista=[[[0],[0],[0]],[[0],[0],[0]],[[0],[0],[0]]]
x=raw_input('please enter 3 words: ')
words=x.split()
if len(words)!=3:
    print('error!!! enter 3 words ')
else:
    i=0
    c=0
    while i<3:
        for m in range(len(words[i])):
           if words[i][m] in ['a','e','i','o','u']:
                       lista.insert([i][0][c],words[i][m])
                       lista.insert([i][0][-1],0)
                       c=c+1
           elif words[i][m] in ['b','c','d','f','g','h','j','k','l','m']:
                       lista.insert([i][1][c],words[i][m])
                       lista.insert([i][1][-1],0)
                       c=c+1
           else:
                       lista.insert([i][2][c],words[i][m])
                       lista.insert([i][2][-1],0)
                       c=c+1
        i=i+1
    lista=(set(lista[1][1],lista[1][2],lista[1][3],lista[2][1],lista[2][2],lista[2][3],lista[3][1],lista[3][2],lista[3][3]))
    lista=(tuple(lista[1],lista[2],lista[3]))
    print lista
lista=[[0]、[0]、[0]、[[0]、[0]、[0]、[[0]、[0]、[0]、[0]]
x=原始输入('请输入3个单词:')
words=x.split()
如果连(话)=三:
打印('错误!!!输入3个单词')
其他:
i=0
c=0

虽然我我认为
行lista.insert([i][1][c],单词[i][m])
(以及与其他索引类似的行)并没有达到您的意图。在尝试计算
[i][1][c]
时,您会遇到一个错误,而不是将
单词[i][m]
插入
列表[i][1][c]
。该子表达式创建一个单元素列表(
[i]
),然后尝试访问索引
1
处的值,但该值无效

我想您可能需要使用类似于
lista[I][1].append(words[I][m])

但是,如果您直接迭代列表和字符串,而不是使用
range
s和索引,则会容易得多:

output = []

for word in words:
    vowels = set()
    consonants1 = set()
    consonants2 = set()

    for character in word:
        if character in "aeiou":
            vowels.add(character)
        elif character in "bcdfghjklm":
            consonants1.add(character)
        else:
            consonants2.add(character)

    output.append([vowels, consonants1, consonants2])
输出:

[(set(['e', 'o']), set(['l']), set(['p'])), 
(set(['e', 'o']), set(['j']), set(['n', 'y'])), 
(set(['o', 'a', 'i']), set(['g', 'm']), set(['p', 'r', 'n']))]
[[set(['e', 'o']), set(['l']), set(['p'])], [set(['e', 'o']), set(['j']), set(['y', 'n'])], [set(['a', 'i', 'o']), set(['m', 'g']), set(['p', 'r', 'n'])]]
[(set(['e', 'o']), set(['l']), set(['p'])), (set(['e', 'o']), set(['j']), set(['y', 'n'])), (set(['a', 'i', 'o']), set(['m', 'g']), set(['p', 'r', 'n']))]
替换上面的
words=“people levely programming”.split()
,使其具有交互性


你在使用已有的编程经验(也许C++)的人中,使用范围和索引来访问项目是很普遍的。,在Python中,大多数内容都是可编辑的。您可以在列表上使用for循环,它将返回每个项目,您可以在字符串上迭代,它将返回每个字符。

您建议的输出在我看来似乎是错误的。
n
的集合中没有
n
。另外,您根据什么对辅音进行分组?您的是3d数组,您将使用4个索引来访问it@mu無 我想我把
'n'
放在了它应该放的地方。请解释一下如何排序。我理解排序的方式是:先是元音,然后是
b
m
辅音,然后是
n
z
辅音。