您好,我正在尝试重新排列 2d 列表,以便在不使用内置函数的情况下处理名称重复。但是当我去打印我得到这个错误
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']]