Reputation: 2612
I don't know if masks can only be applied to strings or numbers so I included both in my title (since I can always translate between the two later).
Let's say I have a mask mask = '001001'
and I want to somehow say "for all positions in the mask that equal 1, apply X to those same positions in this other string".
For instance say I have a number 123456 and I apply that mask to it and want to set the digits to 0. I'd get 120450.
Apologies if I am not making sense. Please suggest an ideal mask type if my earlier string example is not the best, and an easy way to use it to apply changes to a separate number/string.
Upvotes: 2
Views: 7549
Reputation: 251051
In [272]: mask = '001001'
In [273]: num=123456
In [274]: "".join(x if y!='1' else '0' for x,y in zip(str(num),mask))
Out[274]: '120450'
use itertools.izip_longest()
if the number and the mask string are of different length:
In [277]: mask = '001001'
In [278]: num=12345678
In [279]: "".join(x if y!='1' else '0' for x,y in izip_longest(str(num),mask,fillvalue="#"))
Out[279]: '12045078'
Upvotes: 7