myinput = [1,2,3]
RESULT = [[5,6], [4,5,8]]
RESULT.append(myinput)
l = [5,6]
if l not in RESULT:
RESULT.append(l)
else:
# Do something with RESULT
pass # or
# print('Duplicate not appended')
print(f'RESULT: {RESULT}')
raise(Exception(f'{l} is a duplicate and thus was not appended'))
输出:
RESULT: [[5, 6], [4, 5, 8], [1, 2, 3]]
Traceback (most recent call last):
File "main.py", line 15, in <module>
raise(Exception(f'{l} is a duplicate and thus was not appended'))
Exception: [5, 6] is a duplicate and thus was not appended
最简单的解决方案可能是使用
if
语句首先检查[5,6]
是否不在RESULT
中,如果不在,则append
检查它,否则继续,可能会向用户报告它是重复的,而不是附加的:输出:
您可以使用^{} :
然后检查:
相关问题 更多 >
编程相关推荐