Python: Picking an element without replacement
Posted
by wpeters
on Stack Overflow
See other posts from Stack Overflow
or by wpeters
Published on 2010-04-06T18:42:21Z
Indexed on
2010/04/06
18:53 UTC
Read the original article
Hit count: 143
python
I would like to slice random letters from a string.
Given s="howdy"
I would like to pick elements from 's' without replacement but keep the index number.
For example
>>> random.sample(s,len(s))
['w', 'h', 'o', 'd', 'y']
is close to what I want, but I would actually prefer something like
[('w',2), ('h',0), ('o',1), ('d',3), ('y',4)]
with letter-index pairs. This is important because the same letter appears in 's' more than once. ie) "letter" where 't' appears twice but I need to distinguish the first 't' from the 'second'.
Ideally I actually only need to pick letters as I need them but scrambling and calculating all the letters in a list (as shown above) is ok.
© Stack Overflow or respective owner