我有一个在Python中生成的列表,是程序的一部分。我非常确定这些元素都不同,并用assertion进行了检查。
目前我这样做:
如果有两个元素:
try:
assert(x[0] != x[1])
except:
print debug_info
raise Exception("throw to caller")
如果有三个:
try:
assert(x[0] != x[1])
assert(x[0] != x[2])
assert(x[1] != x[2])
except:
print debug_info
raise Exception("throw to caller")
如果我要处理四个元素的话,我会疯掉。
有没有更好的方法来确保列表中的所有元素都是唯一的?