Skip to content

Instantly share code, notes, and snippets.

@oyakata
Created September 9, 2015 04:53
Show Gist options
  • Select an option

  • Save oyakata/7105807ddaaf489b9089 to your computer and use it in GitHub Desktop.

Select an option

Save oyakata/7105807ddaaf489b9089 to your computer and use it in GitHub Desktop.
文字列のN割をランダムにマスキング
# -*- coding:utf-8 -*-
import random
def main(text, n):
length = len(text)
count = int(length * n / 10)
sample = random.sample(range(length), count)
L = list(text)
for i in sample:
L[i] = '*'
return ''.join(L)
if __name__ == '__main__':
print main('bird in the hand is worth two in the bush', 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment