Non-unique Elements

來源:互聯網
上載者:User

標籤:blog   使用   io   for   ar   div   amp   log   

Non-unique Elements

 

You are given a non-empty list of integers (X). For this task, you should return a list consisting of only the non-unique elements in this list. To do so you will need to remove all unique elements (elements which are contained in a given list only once). When solving this task, do not change the order of the list. Example: [1, 2, 3, 1, 3] 1 and 3 non-unique elements and result will be [1, 3, 1, 3].

 

題目大義:將數組中唯一的元素清除

還是C語言的思想,直接粗暴,首先是遍曆求出唯一的元素,再使用數組的remove方法

 

 1 def checkio(data): 2      3     only = [] 4  5     for each in data: 6         count = 0 7         for other in data: 8             if each == other: 9                 count += 110 11         if count == 1:12             only.append(each)13 14     for each in only:15         data.remove(each)16 17     return data

 

當然我們有更進階的方法list = [x for x in data if x.count > 1],簡單明了,使用了數組的另一種構造方法,使用了數組count方法

 

1 def checkio(data):2     list = [x for x in data if data.count(x) > 1]3     return list

 

新技能get

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.