Reputation: 473
So I have string such as:
id = 'ee9baadc-46bc-4486-a78b-830f5b2bcfc9'
How can I go about randomizing the characters of the string, but keeping the dashes in the same location, such as:
id2 = '9eebaadc-46bc-4846-7a8b-380f5b2bcf9c'
This is what I have currently, but it scrambles the dash locations as well:
id2 = ''.join(random.sample(id,len(id)))
Upvotes: 1
Views: 30
Reputation: 56965
You can randomize each section and preserve dash locations using split
(on dashes), sample
to randomize each sub-list and a couple of join
s to tie it all back together:
from random import sample
id_ = 'ee9baadc-46bc-4486-a78b-830f5b2bcfc9'
id_ = "-".join("".join(sample(s, len(s))) for s in id_.split("-"))
Avoid calling variables id
--it overwrites a builtin function.
Upvotes: 1
Reputation: 473
base_id = 'ee9baadc-46bc-4486-a78b-830f5b2bcfc9'
base = base_id.split('-')[0]
random_base = ''.join(random.sample(base,len(base)))
id = re.sub(r'ee9baadc', random_base, base_id)
this will suffice, randomizes first set of chars...if I wanted to, I could iterate on that over the entire string.
Upvotes: 0