亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 python 的一行代碼中評估列表

在 python 的一行代碼中評估列表

交互式愛情 2023-05-09 14:51:39
我創建了一個函數來評估列表是否有重復項: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



查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號