[Tutor] beginning to code

alister alister.ware at ntlworld.com
Tue Sep 12 11:47:06 EDT 2017


On Tue, 12 Sep 2017 08:03:58 -0700, Rick Johnson wrote:

> Chris Angelico wrote:
>> Rick Johnson wrote:
>> > Ruby:
>> >     farray = [1.5, 1.9, 2.0, 1.0]
>> >     uniqueIntegers = farray.map{|f| f.to_i()}.uniq.length
>> >
>> > Python:
>> >     flist = [1.5, 1.9, 2.0, 1.0]
>> >     uniqueIntegers = len(set(map(lambda f:int(f), flist)))
>>
>> Python:
>>
>> floats = [1.5, 1.9, 2.0, 1.0]
>> unique_integers = len(set(int(f) for f in floats))
>>
>> or:
>>
>> unique_integers = len(set(map(int, floats))
>>
>> If you're going to use Python, at least use it right.
> 
> Okay, you've replaced my map function with an implicit list
> comprehension (aka: generator expression)... so how is either more
> "right" than the other? What is your justification that your example is
> "right", and mine is not?
> 
> (1) Is is a speed issue? Then prove it.
> 
> (2) Is it a readability issue? If so, then that's an opinion _you_ get
> to have.
> 
> (3) Is a matter of "python purity"?  If so, then map should be removed
> from the language. And don't forget to remove reduce and filter while
> you're at it. And then, you may want to grab a shield, because the
> functional fanboys will be all over you like white on rice!
> 
> (4) Something else...?

for me the fact that you have had to resort to a lambda when the other 
solutions show it is unnecessary do it for me.

I guess that falls into option 2 but it is an opinion that I can at least 
offer some justification for, especially considering you were using it as 
an example of how ruby was cleaner than python.

were i to be less generous I would suggest that you had deliberately 
picked the worst python method you could think of to make the point



-- 
"jackpot:  you may have an unneccessary change record"
-- message from "diff"



More information about the Python-list mailing list