On 3/7/2017, Sayth Renshaw wrote:

> I have got this dictionary comprehension and it
> works but how can I do it better?
> 
> from collections import Counter
> 
> def find_it(seq):
>      counts = dict(Counter(seq))
>      a = [(k, v) for k,v in counts.items() if v % 3 == 0]
>      return a[0][0]
> 
> test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]
> 
> so this returns 5 which is great and the point
> of the problem I was doing.
> 
> Can also do it like this
> def find_it(seq):
>      counts = dict(Counter(seq))
>      a = [(k) for k,v in counts.items() if v % 3 == 0]
>      return a[0]
> 
> But the given problem states there will always
> only be one number appearing an odd number of
> times given that is there a neater way to get
> the answer?


[20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5].group_by{|x| x}.
find{|k,v| v.size.odd?}.first

 ===>
5

-- 
Goyim were born only to serve us....  They will work, they will plow, they will
reap. We will sit like an effendi and eat. --- Rabbi Ovadia Yosef
web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx?id=191782
archive.org/download/DavidDuke_videos/TopRabbiExposesJewishRacism-cybsdwjezqi.ogv
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to