Home>

The function should return such values

checkio ([1, 2, 3, 1, 3])== [1, 3, 1, 3]
checkio ([1, 2, 3, 4, 5])== []
checkio ([5, 5, 5, 5, 5])== [5, 5, 5, 5, 5]
checkio ([10, 9, 10, 10, 9, 8])== [10, 9, 10, 10, 9]

I want to solve this problem without using only for loops nested within each other. I tried to solve the problem using filter, but I encountered an error ('int' object has no attribute 'count'), and we also understand a partial misunderstanding of how the lambda function works:

def checkio (data: list) ->
 list:
    data= filter (lambda x: x.count (x) >
1, data)
    return data
  • Answer # 1

    well, almost done

    were mistaken only in 1 place -instead ofx.count (x)it was necessary to indicatedata.count (x)

    well, it would be nice afterfiltertranslate the result into a list, since initially it is just an iterable object

    def checkio (data: list) ->
     list:
        return list (filter (lambda x: data.count (x) >
    1, data))
    print (checkio ([1, 2, 3, 1, 3]))
    print (checkio ([1, 2, 3, 4, 5]))
    print (checkio ([5, 5, 5, 5, 5]))
    print (checkio ([10, 9, 10, 10, 9, 8]))
    

    Exactly, thanks a lot, could you tell me what is the difference between list (solution) and [solution], it seems the same thing, but the second option does not work

    fireMouse2021-11-20 13:02:23

    list (solution) will be equivalent to [* solution]

    GrAnd2021-11-20 13:04:19

    thanks a lot

    fireMouse2021-11-20 13:09:27
  • Answer # 2

    well, almost done

    were mistaken only in 1 place -instead ofx.count (x)it was necessary to indicatedata.count (x)

    well, it would be nice afterfiltertranslate the result into a list, since initially it is just an iterable object

    def checkio (data: list) ->
     list:
        return list (filter (lambda x: data.count (x) >
    1, data))
    print (checkio ([1, 2, 3, 1, 3]))
    print (checkio ([1, 2, 3, 4, 5]))
    print (checkio ([5, 5, 5, 5, 5]))
    print (checkio ([10, 9, 10, 10, 9, 8]))
    

    Exactly, thanks a lot, could you tell me what is the difference between list (solution) and [solution], it seems the same thing, but the second option does not work

    fireMouse2021-11-20 13:02:23

    list (solution) will be equivalent to [* solution]

    GrAnd2021-11-20 13:04:19

    thanks a lot

    fireMouse2021-11-20 13:09:27