我創建了一個函數來評估列表是否有重復項:def duplica(list_to_check): if len(set(list_to_check)) != len(list_to_check): print('there are duplicates inside the list') result = 0 else: result = 1 return resultprint(duplica([1, 1, 2]))##test it:there are duplicates inside the list 0我想知道是否有任何其他方法可以使用僅一行代碼(例如 lambda 或 map)來評估列表
1 回答

汪汪一只貓
TA貢獻1898條經驗 獲得超8個贊
如果您只需要值:
0 if len(set(list_to_check)) != len(list_to_check) else 1
甚至更好():(由:Olvin Roght
在評論中提供)
int(len(set(list_to_check)) == len(list_to_check))
打?。?/p>
(0,print('there are duplicates inside the list'))[0] if len(set(list_to_check)) != len(list_to_check) else 1
添加回答
舉報
0/150
提交
取消