python四種方法實現去除列表中的重複元素

來源:互聯網
上載者:User

標籤:重複   get   lse   start   nbsp   方式   app   result   lis   

轉載:76201975

  1. def func1(one_list):  
  2.     ‘‘‘‘‘ 
  3.     使用集合,個人最常用 
  4.     ‘‘‘  
  5.     return list(set(one_list))  
  6.   
  7.   
  8. def func2(one_list):  
  9.     ‘‘‘‘‘ 
  10.     使用字典的方式 
  11.     ‘‘‘  
  12.     return {}.fromkeys(one_list).keys()  
  13.   
  14.   
  15. def func3(one_list):  
  16.     ‘‘‘‘‘ 
  17.     使用列表推導的方式 
  18.     ‘‘‘  
  19.     temp_list=[]  
  20.     for one in one_list:  
  21.         if one not in temp_list:  
  22.             temp_list.append(one)  
  23.     return temp_list  
  24.   
  25.   
  26. def func4(one_list):  
  27.     ‘‘‘‘‘ 
  28.     使用排序的方法 
  29.     ‘‘‘  
  30.     result_list=[]  
  31.     temp_list=sorted(one_list)  
  32.     i=0  
  33.     while i<len(temp_list):  
  34.         if temp_list[i] not in result_list:  
  35.             result_list.append(temp_list[i])  
  36.         else:  
  37.             i+=1  
  38.     return result_list  
  39.   
  40.   
  41. if __name__ == ‘__main__‘:  
  42.     one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]  
  43.     print func1(one_list)  
  44.     print func2(one_list)  
  45.    print func3(one_list)     
  46.  print func4(one_list)  

python四種方法實現去除列表中的重複元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.