[Tutor] beginning to code

Chris Angelico rosuav at gmail.com
Tue Sep 12 11:17:59 EDT 2017


On Wed, Sep 13, 2017 at 1:03 AM, Rick Johnson
<rantingrickjohnson at gmail.com> 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?
>

You had:

uniqueIntegers = len(set(map(lambda f:int(f), flist)))

I replaced it with:

unique_integers = len(set(map(int, floats))

Aside from the variable names, the significant difference here is that
I pass int to map directly, where you wrap it up in a useless lambda
function:

lambda f: int(f)

Of course Python is going to look worse if you add stuff like that to your code.

ChrisA



More information about the Python-list mailing list