Re: Moderator please remove [Re: Better way to do this dict comprehesion]

2017-04-01 Thread Skip Montanaro
Passed along to postmas...@python.org, who generally takes care of this
sort of thing.

Skip

On Apr 1, 2017 11:19 AM, "Steve D'Aprano" 
wrote:

> Can one of the mailing list moderators please remove this person's post for
> including a link to an anti-Semitic video by David Duke?
>
>
>
> On Sat, 1 Apr 2017 07:13 pm, Robert L. wrote:
>
> Goyim were born [...]
> web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld[...]
> archive.org/download/DavidDuke_videos/[...]
>
> I've left in just enough of the original content so the moderator can see
> what I'm objecting to.
>
> Robert, if you're reading this, take your anti-semitism elsewhere, we're
> not
> interested. If you want to discuss Python, you are welcome here, but if you
> use it as a platform for bigotry, you aren't.
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Moderator please remove [Re: Better way to do this dict comprehesion]

2017-04-01 Thread Steve D'Aprano
Can one of the mailing list moderators please remove this person's post for
including a link to an anti-Semitic video by David Duke?



On Sat, 1 Apr 2017 07:13 pm, Robert L. wrote:

Goyim were born [...]
web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld[...]
archive.org/download/DavidDuke_videos/[...]

I've left in just enough of the original content so the moderator can see
what I'm objecting to.

Robert, if you're reading this, take your anti-semitism elsewhere, we're not
interested. If you want to discuss Python, you are welcome here, but if you
use it as a platform for bigotry, you aren't.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-04-01 Thread Robert L.
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


Re: Better way to do this dict comprehesion

2017-03-08 Thread Jussi Piitulainen
Sayth Renshaw writes:

>> To find an unpaired number in linear time with minimal space, try
>> stepping through the list and either adding to a set or removing from
>> it. At the end, your set should contain exactly one element. I'll let
>> you write the actual code :)
>> 
>> ChrisA
>
> ChrisA the way it sounds through the list is like filter with map and
> a lambda. http://www.python-course.eu/lambda.php Haven't had a good go
> yet... will see how it goes.

You mean reduce(xor, map(lambda o : {o}. With suitable imports.

I think Chris is suggesting the same thing but in the piecemeal way of
updating a set at each element. You can test for membership, o in res,
and then res.add(o) or res.remove(o) depending on the test.

And you need to say set() to make an empty set, because {} is dict().
Your input sequence is guaranteed non-empty, but it's simply easier to
start with an empty res.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-08 Thread Jussi Piitulainen
Sayth Renshaw writes:

> Peter I really like this
>
> The complete code: 
>
 from collections import Counter 
 def find_it(seq): 
> ... [result] = [k for k, v in Counter(seq).items() if v % 3 == 0] 
> ... return result 

You confirmed to Chris that you want the item that occurs an odd number
of times. The test for that is v % 2 == 1.

Or just v % 2, given that 0 and 1 are considered false and true, resp.

But v % 3 == 0 is something else.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-08 Thread Peter Otten
Sayth Renshaw wrote:

> Peter I really like this
> 
> The complete code:
> 
 from collections import Counter
 def find_it(seq):
> ... [result] = [k for k, v in Counter(seq).items() if v % 3 == 0]
> ... return result
> ...
 test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]
 find_it(test_seq)
> 
> But what may be the smallest thing in this i had no idea I could do
> [result] = blah and get a generator on the return variable that seems
> insane.

Usually this is done with tuples

>>> left, sep, right = "foo.bar".partition(".")  # looks familiar?

rather than the alternative spelling

>>> [left, sep, right] = "foo.bar".partition(".")

However, a 1-tuple is just a trailing comma and easy to overlook, so i 
prefer

>>> items = [42]
>>> [foo] = items
>>> foo
42

over

>>> bar, = items
>>> bar
42


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-08 Thread Sayth Renshaw
Peter I really like this

The complete code: 

>>> from collections import Counter 
>>> def find_it(seq): 
... [result] = [k for k, v in Counter(seq).items() if v % 3 == 0] 
... return result 
... 
>>> test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5] 
>>> find_it(test_seq) 

But what may be the smallest thing in this i had no idea I could do [result] = 
blah and get a generator on the return variable that seems insane.

Cheers

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-08 Thread Peter Otten
Sayth Renshaw wrote:

> Hi
> 
> I have got this dictionary comprehension and it works but how can I do it
> better?

List comprehensions (that's what you have) are nice, but overused.

 
> from collections import Counter
> 
> def find_it(seq):
>  counts = dict(Counter(seq))

There is no need to convert Counter to dict.

>  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?

If you mean there is only one number satisfying the v % 3 == 0 condition 
then there is no need to go through the whole sequence. The clearest way to 
express that is

for k, v in counts.items():
if v % 3 == 0:
return k
raise ValueError

but

it = (k for k, v in counts.items() if v % 3 == 0)
try:
return next(it)
except StopIteration:
pass
raise ValueError

is also possible. The parens (...) instead of [...] make "it" generator 
expression which is evaluated lazily. 

Both alternatives shown above ensure that at least one value satisfies the 
condition "number of occurencies divides by three without rest". If you want 
to play it safe and verify that there is exactly one such key you may keep 
the listcomp, preferably that from your second implementation.

a = [...]
if len(a) != 1:
raise ValueError
return a[0]

or

[result] = a  # will raise a ValueError if len(a) != 1

The complete code:

>>> from collections import Counter
>>> def find_it(seq):
... [result] = [k for k, v in Counter(seq).items() if v % 3 == 0]
... return result
... 
>>> test_seq = [20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5]
>>> find_it(test_seq)
5


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-07 Thread Sayth Renshaw

> > 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?
> 
> Take a step back for a moment. Are you trying to find something that
> appears an odd number of times, or a number of times that counts by
> three? First figure that out, THEN see if there's a better way to do
> what you're doing.

A number that appears an odd number of times.

> 
> To find an unpaired number in linear time with minimal space, try
> stepping through the list and either adding to a set or removing from
> it. At the end, your set should contain exactly one element. I'll let
> you write the actual code :)
> 
> ChrisA

ChrisA the way it sounds through the list is like filter with map and a lambda. 
http://www.python-course.eu/lambda.php Haven't had a good go yet... will see 
how it goes.

Thanks

Sayth

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-07 Thread Chris Angelico
On Wed, Mar 8, 2017 at 1:55 PM, MRAB  wrote:
> Using Counter seems like a simpler way to me. I wouldn't bother about other
> ways unless that way wasn't "good enough" for some reason.

Maybe. But without being sure what the goal is, it's hard to say.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-07 Thread MRAB

On 2017-03-08 02:37, Chris Angelico wrote:

On Wed, Mar 8, 2017 at 1:28 PM, 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?


Take a step back for a moment. Are you trying to find something that
appears an odd number of times, or a number of times that counts by
three? First figure that out, THEN see if there's a better way to do
what you're doing.

To find an unpaired number in linear time with minimal space, try
stepping through the list and either adding to a set or removing from
it. At the end, your set should contain exactly one element. I'll let
you write the actual code :)

Using Counter seems like a simpler way to me. I wouldn't bother about 
other ways unless that way wasn't "good enough" for some reason.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Better way to do this dict comprehesion

2017-03-07 Thread Chris Angelico
On Wed, Mar 8, 2017 at 1:28 PM, 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?

Take a step back for a moment. Are you trying to find something that
appears an odd number of times, or a number of times that counts by
three? First figure that out, THEN see if there's a better way to do
what you're doing.

To find an unpaired number in linear time with minimal space, try
stepping through the list and either adding to a set or removing from
it. At the end, your set should contain exactly one element. I'll let
you write the actual code :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Better way to do this dict comprehesion

2017-03-07 Thread Sayth Renshaw
Hi

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?


Thanks

Sayth
-- 
https://mail.python.org/mailman/listinfo/python-list