tags:

views:

146

answers:

4

Is there a way to append elements to a list randomly, built in function

ex:

def random_append():
     lst = ['a']
     lst.append('b')
     lst.append('c')
     lst.append('d')
     lst.append('e')
     return print lst

this will out put ['a', 'b', 'c', 'd', 'e']

But I want it to add elements randomly and out put something like this: ['b', 'd', 'b', 'e', 'c']

And yes there's a function random.shuffle() but it shuffles a list at once which I don't require, I just want to perform random inserts only.

+9  A: 

If there is supposed to be exactly one of each item

>>> from random import randint
>>> a=[]
>>> for x in "abcde":
...  a.insert(randint(0,len(a)),x)
... 
>>> a
['b', 'a', 'd', 'c', 'e']

If you are allowing duplicates (as the output indicates)

>>> from random import choice
>>> a=[choice("abcde") for x in range(5)]
>>> a
['a', 'b', 'd', 'b', 'a']
gnibbler
Note that this is O(n^2) and populating `a` then using `random.shuffle` is O(n).
Mike Graham
@Mike Graham, presumably the OP wants to do something with the list between inserts, since they don't want to use shuffle.
gnibbler
@gnibbler, I really have a hard time understanding exactly what OP wants from the description as it stands. As the Python axiom goes, *In the face of ambiguity, refuse the temptation to guess.*.
Mike Graham
The OP explicitly says shuffle does not do what they want. So I don't understand how answering the question is guessing
gnibbler
I appealed to the idea of guessing since you *presumed* what the situation was. Presuming that someone has a good, unstated reason for anything they ask about on the internet is not conducive to consistently getting people the best answers.
Mike Graham
@Mike Graham we can't fully depend upon built in Python functions. This is just a small part of a comprehensive algorithm. @gnibbler Thanks for your answer, but this is not exactly what I needed,but it helped me a lot to make up the foundation.
MMRUser
@MMRUser, What does "we can't fully depend on built in Python functions" mean?
Mike Graham
+6  A: 

random.shuffle is probably the best tool for the job. It is simple, obvious, and well-named—it's probably more readable than the other suggestions you will get. Additionally, using it is O(n), but using insert (an O(n) operation) n times is quadratic.

Mike Graham
A: 
from random import choice

n=10
seq=['a','b','c','d']
rstr=[choice(seq) for i in range(n)]
ralu
+1  A: 

If you need to perform single insert in a random position then the already given trivial exapmle works:

from random import randrange, sample

def random_insert(lst, item):
    lst.insert(randrange(len(lst)+1), item)

However if you need to insert k items to a list of length n then using the previously given function is O(n*k + k**2) complexity. However inserting multiple items can be done in linear time O(n+k) if you calculate the target positions ahead of time and rewrite the input list in one go:

def random_insert_seq(lst, seq):
    insert_locations = sample(xrange(len(lst) + len(seq)), len(seq))
    inserts = dict(zip(insert_locations, seq))
    input = iter(lst)
    lst[:] = [inserts[pos] if pos in inserts else next(input)
        for pos in xrange(len(lst) + len(seq))]
Ants Aasma