Is there a way to subtract 3 from every digit of a number?

C W tmrsg11 at gmail.com
Sun Feb 21 09:21:28 EST 2021


I do want to follow up, if I may. In Ming's example,

a = 2342
b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a)))))

What's the best approach to apply to a dataframe column, rather than just
one value?  Here's my attempt using df[col_1''].apply(),
df['col_1'].apply(lambda:a int("".join(map(lambda x: str((int(x)-3)%10)
,list(str(a))))))

Thanks!

On Sun, Feb 21, 2021 at 9:12 AM C W <tmrsg11 at gmail.com> wrote:

> Thanks so much everyone, I appreciate it!
>
> Ming, your solution is awesome. More importantly, very clear explanations
> on how and why. So, I appreciate that.
>
> Thanks again, cheers!
>
> Mike
>
> On Sun, Feb 21, 2021 at 12:08 AM <2QdxY4RzWzUUiLuE at potatochowder.com>
> wrote:
>
>> On 2021-02-20 at 20:49:15 -0800,
>> Dan Stromberg <drsalists at gmail.com> wrote:
>>
>> > On Sat, Feb 20, 2021 at 7:13 PM Ming <ming at pgp.cool> wrote:
>> >
>> > > I just wrote a very short code can fulfill your needs:
>> > >
>> > > a = 2342
>> > > b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a)))))
>> > >
>> > I tend to favor plenty of temporary variables with descriptive names,
>> but
>> > this is indeed short.
>> >
>> > Apart from that, you may find that using a generator expression is
>> shorter
>> > and clearer than map+lambda.  It should allow to additionally eliminate
>> the
>> > list conversion.
>> >
>> > So in the terse form you've got there, it'd be more like:
>> > b =  int(''.join(str((int(x) - 3) % 10) for x in str(a))
>> >
>> > But in real life, I'd try to use descriptive variable names for some of
>> the
>> > subexpressions in that.  This makes reading and debugging simpler,
>> which is
>> > important because the maintenance phase of software is almost always
>> much
>> > longer and costly than the development phase.  And although you could
>> do a
>> > generator expression for each of the different parts of (int(x) - 3) %
>> 10,
>> > I kinda like having a named function for just that piece.
>> >
>> > So maybe:
>> >   def rot_3(character):
>> >       """Convert to int, subtract 3 and mod 10."""
>> >       digit = int(character)
>> >       assert 0 <= digit <= 9
>> >       return (digit - 3) % 10
>> >
>> >
>> >   def descriptive_minus_three_caesar(input_number):
>> >       """Convert to a -3 caesar cypher on an integer."""
>> >       string_number = str(input_number)
>> >       rotated_digits = (rot_3(character) for character in string_number)
>> >       output_string = ''.join(str(digit) for digit in rotated_digits)
>> >       output_number = int(output_string)
>> >       return output_number
>>
>> >>> descriptive_minus_three_caesar('38')
>> 5
>>
>> The problem is underspecified, and the examples are lacking, but based
>> on the phrase "each digit" and the examples that contain a 3, I'd prefer
>> to see "38" become "05."
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>


More information about the Python-list mailing list