我有一个小问题 - 我需要遍历列表列表(文件中的行)并计算行以六或七开头的次数。没问题,但我还需要删除重复的项目,例如,如果我有这个:[6,6,7,7,6],我需要这样做:[6,7,6 ] - 重要的是开关的数量。但不知何故,列表索引总是超出范围。为什么?谢谢!
def number_of_switches(list):
counter = []
for element in li:
if int(element[0]) == 6 or int(element[0]) == 7: counter.append(element[0])
else: pass
i = 0
for i in (0, len(counter)):
if counter[i] == counter[i+1]: counter.remove(counter[i+1])
print 'number of switches is', len(counter)-1 #the first number doesn't count as switch, hence '-1'