0

こんにちは、組み込み関数を使用せずに名前の繰り返しを処理できるように、2 次元リストを再配置しようとしています。しかし、印刷しようとするとこのエラーが発生します

TypeError: unsupported operand type(s) for -: 'list' and 'int'

問題のコードは

if c[i][0]==c[i+1][0] and c[c-1][0]!= c[i][0] :

そして、私が望んでいるコードは、エラーのためにチェックできない私のプログラムを実行します

d = []
d.append(c[0][0])
d.append(c[0][1])
i = 1
while size - 1 :
    # for more multiple repeats,append only classes
    if c[i][0]==c[i+1][0] and c[i-1][0]==c[i][0] :
        d.append(c[i][1])
        d.append(c[i+1][1])
    # for single repeats, append name, and classes
    if c[i][0]==c[i+1][0] and c[c-1][0]!= c[i][0] :
        d.append(c[i][0])
        d.append(c[i][1])
        d.append(c[i+1][1])
    # no  previous repeats, append name and class
    else :
        d.append(c[i][0])
        d.append(c[i][1])
    i = i + 1
print d

前の if ステートメントでは問題が発生しなかったため、なぜエラーが発生するのかわかりません。多分それは!=声明と関係がありますか?コードを実行したい場合、処理中のリストは

[['Adam', 'PHYS 1443'], ['Ashley', 'IE 3312'], ['Ashley', 'PHYS 1443'], ['August', 'PHYS 1444'], ['Baron', 'PHYS 1443'], ['Christopher', 'IE 3301'], ['Christopher', 'CSE 1320'], ['Christopher', 'PHYS 1443'], ['Dylan', 'CSE 1310'], ['Henry', 'PHYS 1444'], ['James', 'IE 3301'], ['James', 'PHYS 1443'], ['Jonathan', 'IE 3312'], ['Krishna', 'CSE 1310'], ['Luis', 'CSE 1310'], ['Michael', 'IE 3301'], ['Nang', 'PHYS 1443'], ['Pramod', 'PHYS 1444'], ['Pramod', 'PHYS 1443'], ['Saroj', 'IE 3301'], ['Saroj', 'MATH 1426'], ['Sol', 'CSE 1310'], ['Timothy', 'MATH 2325'], ['Timothy', 'IE 3301']]
4

2 に答える 2

4

clistあなたがアクセスしているので、int.

おそらくあなたは次のことを意味しました:

if c[i][0]==c[i+1][0] and c[i-1][0]!= c[i][0] :
于 2013-10-29T22:23:32.433 に答える
2

最初のエラーはc、行内の indesx としてリストされます

if c[i][0]==c[i+1][0] and c[c-1][0]!= c[i][0] :

そして、私は他のエラーがあると思います。sizeどこが減っているのかわかりません。

while size - 1 :
     ....

多分あなたは意味した

while size - i:
    ...
于 2013-10-29T22:27:00.890 に答える