[CheckiO系列1]Non-unique Elements

算是做的第一个Python练习了,对我这个菜鸟来说还是学到不少东西的。

题目:

You are given a list of integers. 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].

non-unique-elements

Input: A list of integers.

Output: A list of integers.

我的解答:

def checkio(data):
    #Your code here
    #It's main function. Don't remove this function
    #It's using for auto-testing and must return a result for check.  

    n=0

    data1=list()
    while n<len(data):
        if(data.count(data[n])>1):
            data1.append(data[n]) 
        n=n+1
    return data1

改进:

基于对语言的不熟悉,比较好的做法应该是把while语句改成for语句。


另有高人解法:

checkio=lambda d:[x for x in d if d.count(x)>1]

学习到的相关内容:list,lamda,链表推导式

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值