How do I retrieve an item at random from the following list?
foo = ['a', 'b', 'c', 'd', 'e']
Use random.choice():
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():
import secrets
foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))
secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:
import random
secure_random = random.SystemRandom()
print(secure_random.choice(foo))
If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.
import random
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).
Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.
EDIT: Using Secrets
As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:
import secrets # imports secure module.
secure_random = secrets.SystemRandom() # creates a secure random object.
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = secure_random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
EDIT: Pythonic One-Liner
If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.
import random
first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
If you also need the index, use random.randrange
from random import randrange
random_index = randrange(len(foo))
print(foo[random_index])
As of Python 3.6 you can use the secrets module, which is preferable to the random module for cryptography or security uses.
To print a random element from a list:
import secrets
foo = ['a', 'b', 'c', 'd', 'e']
print(secrets.choice(foo))
To print a random index:
print(secrets.randbelow(len(foo)))
For details, see PEP 506.
I propose a script for removing randomly picked up items off a list until it is empty:
Maintain a set and remove randomly picked up element (with choice) until list is empty.
s=set(range(1,6))
import random
while len(s)>0:
s.remove(random.choice(list(s)))
print(s)
Three runs give three different answers:
>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])
>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
foo = ['a', 'b', 'c', 'd', 'e']
number_of_samples = 1
In Python 2:
random_items = random.sample(population=foo, k=number_of_samples)
In Python 3:
random_items = random.choices(population=foo, k=number_of_samples)
NumPy solution: numpy.random.choice
For this question, it works the same as the accepted answer (import random; random.choice()), but I added it because the programmer may have imported NumPy already (like me)
And also there are some differences between the two methods that may concern your actual use case.
import numpy as np
np.random.choice(foo) # randomly selects a single item
For reproducibility, you can do:
np.random.seed(123)
np.random.choice(foo) # first call will always return 'c'
For samples of one or more items, returned as an array, pass the size argument:
np.random.choice(foo, 5) # sample with replacement (default)
np.random.choice(foo, 5, False) # sample without replacement
If you need the index, just use:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print int(random.random() * len(foo))
print foo[int(random.random() * len(foo))]
random.choice does the same:)
How to randomly select an item from a list?
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
If you want close to truly random, then I suggest secrets.choice from the standard library (New in Python 3.6.):
>>> from secrets import choice # Python 3 only
>>> choice(list('abcde'))
'c'
The above is equivalent to my former recommendation, using a SystemRandom object from the random module with the choice method - available earlier in Python 2:
>>> import random # Python 2 compatible
>>> sr = random.SystemRandom()
>>> foo = list('abcde')
>>> foo
['a', 'b', 'c', 'd', 'e']
And now:
>>> sr.choice(foo)
'd'
>>> sr.choice(foo)
'e'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'b'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'c'
>>> sr.choice(foo)
'c'
If you want a deterministic pseudorandom selection, use the choice function (which is actually a bound method on a Random object):
>>> random.choice
<bound method Random.choice of <random.Random object at 0x800c1034>>
It seems random, but it's actually not, which we can see if we reseed it repeatedly:
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
A comment:
This is not about whether random.choice is truly random or not. If you fix the seed, you will get the reproducible results -- and that's what seed is designed for. You can pass a seed to SystemRandom, too. sr = random.SystemRandom(42)
Well, yes you can pass it a "seed" argument, but you'll see that the SystemRandom object simply ignores it:
def seed(self, *args, **kwds):
"Stub method. Not used for a system random number generator."
return None
I usually use the random module for working with lists and randomization:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
In short, use random.sample method
The sample method returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples.
import random
lst = ['a', 'b', 'c', 'd', 'e']
random.seed(0) # remove this line, if you want different results for each run
rand_lst = random.sample(lst,3) # 3 is the number of sample you want to retrieve
print(rand_lst)
Output:['d', 'e', 'a']
here is a running code
https://onecompiler.com/python/3xem5jjvz
This is the code with a variable that defines the random index:
import random
foo = ['a', 'b', 'c', 'd', 'e']
randomindex = random.randint(0,len(foo)-1)
print (foo[randomindex])
## print (randomindex)
This is the code without the variable:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print (foo[random.randint(0,len(foo)-1)])
And this is the code in the shortest and smartest way to do it:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
(python 2.7)
Random item selection:
import random
my_list = [1, 2, 3, 4, 5]
num_selections = 2
new_list = random.sample(my_list, num_selections)
To preserve the order of the list, you could do:
randIndex = random.sample(range(len(my_list)), n_selections)
randIndex.sort()
new_list = [my_list[i] for i in randIndex]
Duplicate of https://stackoverflow.com/a/49682832/4383027
You could just:
from random import randint
foo = ["a", "b", "c", "d", "e"]
print(foo[randint(0,4)])
This may already be an answer, but you can use random.shuffle. Example:
import random
foo = ['a', 'b', 'c', 'd', 'e']
random.shuffle(foo)
The recommended numpy way is now to use an explicit RNG:
from numpy.random import default_rng
rng = default_rng()
rng.choice(foo)
We can also do this using randint.
from random import randint
l= ['a','b','c']
def get_rand_element(l):
if l:
return l[randint(0,len(l)-1)]
else:
return None
get_rand_element(l)
Related
How do I retrieve an item at random from the following list?
foo = ['a', 'b', 'c', 'd', 'e']
Use random.choice():
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use secrets.choice():
import secrets
foo = ['battery', 'correct', 'horse', 'staple']
print(secrets.choice(foo))
secrets is new in Python 3.6. On older versions of Python you can use the random.SystemRandom class:
import random
secure_random = random.SystemRandom()
print(secure_random.choice(foo))
If you want to randomly select more than one item from a list, or select an item from a set, I'd recommend using random.sample instead.
import random
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
If you're only pulling a single item from a list though, choice is less clunky, as using sample would have the syntax random.sample(some_list, 1)[0] instead of random.choice(some_list).
Unfortunately though, choice only works for a single output from sequences (such as lists or tuples). Though random.choice(tuple(some_set)) may be an option for getting a single item from a set.
EDIT: Using Secrets
As many have pointed out, if you require more secure pseudorandom samples, you should use the secrets module:
import secrets # imports secure module.
secure_random = secrets.SystemRandom() # creates a secure random object.
group_of_items = {'a', 'b', 'c', 'd', 'e'} # a sequence or set will work here.
num_to_select = 2 # set the number to select here.
list_of_random_items = secure_random.sample(group_of_items, num_to_select)
first_random_item = list_of_random_items[0]
second_random_item = list_of_random_items[1]
EDIT: Pythonic One-Liner
If you want a more pythonic one-liner for selecting multiple items, you can use unpacking.
import random
first_random_item, second_random_item = random.sample({'a', 'b', 'c', 'd', 'e'}, 2)
If you also need the index, use random.randrange
from random import randrange
random_index = randrange(len(foo))
print(foo[random_index])
As of Python 3.6 you can use the secrets module, which is preferable to the random module for cryptography or security uses.
To print a random element from a list:
import secrets
foo = ['a', 'b', 'c', 'd', 'e']
print(secrets.choice(foo))
To print a random index:
print(secrets.randbelow(len(foo)))
For details, see PEP 506.
I propose a script for removing randomly picked up items off a list until it is empty:
Maintain a set and remove randomly picked up element (with choice) until list is empty.
s=set(range(1,6))
import random
while len(s)>0:
s.remove(random.choice(list(s)))
print(s)
Three runs give three different answers:
>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])
>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
foo = ['a', 'b', 'c', 'd', 'e']
number_of_samples = 1
In Python 2:
random_items = random.sample(population=foo, k=number_of_samples)
In Python 3:
random_items = random.choices(population=foo, k=number_of_samples)
NumPy solution: numpy.random.choice
For this question, it works the same as the accepted answer (import random; random.choice()), but I added it because the programmer may have imported NumPy already (like me)
And also there are some differences between the two methods that may concern your actual use case.
import numpy as np
np.random.choice(foo) # randomly selects a single item
For reproducibility, you can do:
np.random.seed(123)
np.random.choice(foo) # first call will always return 'c'
For samples of one or more items, returned as an array, pass the size argument:
np.random.choice(foo, 5) # sample with replacement (default)
np.random.choice(foo, 5, False) # sample without replacement
If you need the index, just use:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print int(random.random() * len(foo))
print foo[int(random.random() * len(foo))]
random.choice does the same:)
How to randomly select an item from a list?
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
If you want close to truly random, then I suggest secrets.choice from the standard library (New in Python 3.6.):
>>> from secrets import choice # Python 3 only
>>> choice(list('abcde'))
'c'
The above is equivalent to my former recommendation, using a SystemRandom object from the random module with the choice method - available earlier in Python 2:
>>> import random # Python 2 compatible
>>> sr = random.SystemRandom()
>>> foo = list('abcde')
>>> foo
['a', 'b', 'c', 'd', 'e']
And now:
>>> sr.choice(foo)
'd'
>>> sr.choice(foo)
'e'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'b'
>>> sr.choice(foo)
'a'
>>> sr.choice(foo)
'c'
>>> sr.choice(foo)
'c'
If you want a deterministic pseudorandom selection, use the choice function (which is actually a bound method on a Random object):
>>> random.choice
<bound method Random.choice of <random.Random object at 0x800c1034>>
It seems random, but it's actually not, which we can see if we reseed it repeatedly:
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
>>> random.seed(42); random.choice(foo), random.choice(foo), random.choice(foo)
('d', 'a', 'b')
A comment:
This is not about whether random.choice is truly random or not. If you fix the seed, you will get the reproducible results -- and that's what seed is designed for. You can pass a seed to SystemRandom, too. sr = random.SystemRandom(42)
Well, yes you can pass it a "seed" argument, but you'll see that the SystemRandom object simply ignores it:
def seed(self, *args, **kwds):
"Stub method. Not used for a system random number generator."
return None
I usually use the random module for working with lists and randomization:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
In short, use random.sample method
The sample method returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples.
import random
lst = ['a', 'b', 'c', 'd', 'e']
random.seed(0) # remove this line, if you want different results for each run
rand_lst = random.sample(lst,3) # 3 is the number of sample you want to retrieve
print(rand_lst)
Output:['d', 'e', 'a']
here is a running code
https://onecompiler.com/python/3xem5jjvz
This is the code with a variable that defines the random index:
import random
foo = ['a', 'b', 'c', 'd', 'e']
randomindex = random.randint(0,len(foo)-1)
print (foo[randomindex])
## print (randomindex)
This is the code without the variable:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print (foo[random.randint(0,len(foo)-1)])
And this is the code in the shortest and smartest way to do it:
import random
foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
(python 2.7)
Random item selection:
import random
my_list = [1, 2, 3, 4, 5]
num_selections = 2
new_list = random.sample(my_list, num_selections)
To preserve the order of the list, you could do:
randIndex = random.sample(range(len(my_list)), n_selections)
randIndex.sort()
new_list = [my_list[i] for i in randIndex]
Duplicate of https://stackoverflow.com/a/49682832/4383027
You could just:
from random import randint
foo = ["a", "b", "c", "d", "e"]
print(foo[randint(0,4)])
This may already be an answer, but you can use random.shuffle. Example:
import random
foo = ['a', 'b', 'c', 'd', 'e']
random.shuffle(foo)
The recommended numpy way is now to use an explicit RNG:
from numpy.random import default_rng
rng = default_rng()
rng.choice(foo)
We can also do this using randint.
from random import randint
l= ['a','b','c']
def get_rand_element(l):
if l:
return l[randint(0,len(l)-1)]
else:
return None
get_rand_element(l)
Say I have a list of options and I want to pick a certain number randomly.
In my case, say the options are in a list ['a', 'b', 'c', 'd', 'e'] and I want my script to return 3 elements.
However, there is also the case of two options that cannot appear at the same time. That is, if option 'a' is picked randomly, then option 'b' cannot be picked. And the same applies the other way round.
So valid outputs are: ['a', 'c', 'd'] or ['c', 'd', 'b'], while things like ['a', 'b', 'c'] would not because they contain both 'a' and 'b'.
To fulfil these requirements, I am fetching 3 options plus another one to compensate a possible discard. Then, I keep a set() with the mutually exclusive condition and keep removing from it and check if both elements have been picked or not:
import random
mutually_exclusive = set({'a', 'b'})
options = ['a', 'b', 'c', 'd', 'e']
num_options_to_return = 3
shuffled_options = random.sample(options, num_options_to_return + 1)
elements_returned = 0
for item in shuffled_options:
if elements_returned >= num_options_to_return:
break
if item in mutually_exclusive:
mutually_exclusive.remove(item)
if not mutually_exclusive:
# if both elements have appeared, then the set is empty so we cannot return the current value
continue
print(item)
elements_returned += 1
However, I may be overcoding and Python may have better ways to handle these requirements. Going through random's documentation I couldn't find ways to do this out of the box. Is there a better solution than my current one?
One way to do this is use itertools.combinations to create all of the possible results, filter out the invalid ones and make a random.choice from that:
>>> from itertools import combinations
>>> from random import choice
>>> def is_valid(t):
... return 'a' not in t or 'b' not in t
...
>>> choice([
... t
... for t in combinations('abcde', 3)
... if is_valid(t)
... ])
...
('c', 'd', 'e')
Maybe a bit naive, but you could generate samples until your condition is met:
import random
options = ['a', 'b', 'c', 'd', 'e']
num_options_to_return = 3
mutually_exclusive = set({'a', 'b'})
while True:
shuffled_options = random.sample(options, num_options_to_return)
if all (item not in mutually_exclusive for item in shuffled_options):
break
print(shuffled_options)
You can restructure your options.
import random
options = [('a', 'b'), 'c', 'd', 'e']
n_options = 3
selected_option = random.sample(options, n_options)
result = [item if not isinstance(item, tuple) else random.choice(item)
for item in selected_option]
print(result)
I would implement it with sets:
import random
mutually_exclusive = {'a', 'b'}
options = ['a', 'b', 'c', 'd', 'e']
num_options_to_return = 3
while True:
s = random.sample(options, num_options_to_return)
print('Sample is', s)
if not mutually_exclusive.issubset(s):
break
print('Discard!')
print('Final sample:', s)
Prints (for example):
Sample is ['a', 'b', 'd']
Discard!
Sample is ['b', 'a', 'd']
Discard!
Sample is ['e', 'a', 'c']
Final sample: ['e', 'a', 'c']
I created the below function and I think it's worth sharing it too ;-)
def random_picker(options, n, mutually_exclusives=None):
if mutually_exclusives is None:
return random.sample(options, n)
elif any(len(pair) != 2 for pair in mutually_exclusives):
raise ValueError('Lenght of pairs of mutually_exclusives iterable, must be 2')
res = []
while len(res) < n:
item_index = random.randint(0, len(options) - 1)
item = options[item_index]
if any(item == exc and pair[-(i - 1)] in res for pair in mutually_exclusives
for i, exc in enumerate(pair)):
continue
res.append(options.pop(item_index))
return res
Where:
options is the list of available options to pick from.
n is the number of items you want to be picked from options
mutually_exclusives is an iterable containing tuples pairs of mutually exclusive items
You can use it as follows:
>>> random_picker(['a', 'b', 'c', 'd', 'e'], 3)
['c', 'e', 'a']
>>> random_picker(['a', 'b', 'c', 'd', 'e'], 3, [('a', 'b')])
['d', 'b', 'e']
>>> random_picker(['a', 'b', 'c', 'd', 'e'], 3, [('a', 'b'), ('a', 'c')])
['e', 'd', 'a']
import random
l = [['a','b'], ['c'], ['d'], ['e']]
x = [random.choice(i) for i in random.sample(l,3)]
here l is a two-dimensional list, where the fist level reflects an and relation and the second level an or relation.
I have a dictionary with name dic in python, where the keys are strings and the corresponding values are integers.
dic ={ 'a' : 2, 'b' : 3, 'c':4, 'd':5 }.
Note that 2+3+4+5=14.
Now I need to impose an order to this dictionary, say the order is from 'a', to 'b', and then to 'c'. Then, I randomly generate 3 numbers from the list of intergers from 1 to 14, say, 2, 9, and 10. Then, these 3 numbers would correspond to the keys 'a', 'c', and 'd'.
All I can think of is to use dic.keys and dic.values to create two corresponding lists and then calculate the presums and do it in a very trivial way.
Is there a default python function to do this?
Maybe you can consider using random.sample like so.
>>> import random
>>> p = 2*['a'] + 3*['b'] + 4*['c'] + 5*['d']
>>> random.sample(p, 3)
['b', 'b', 'a']
From the docs, random.sample returns a k length list of unique elements chosen from the population sequence or set. It is used for random sampling without replacement. Therefore, the largest sample you can get from a population of size 14 is size 14, and a sample of size 14 is guaranteed to be a permutation of p.
Alternatively you can use your method of selecting an integer between 1 and 14 inclusive to make weighted random choices using p like this:
>>> k = random.choice(range(1, 15))
>>> p[k-1]
'b'
or, if you don't need the "index" of the selected element:
>>> random.choice(p)
'c'
However, note that by using random.choice repeatedly, you will be sampling with replacement (unless you have some mechanism of removing selecting elements from the population). This may be what you want though.
To construct your population p dynamically using your dictionary, you can do something like this:
>>> sum((w*[k] for k, w in dic.items()), [])
['d', 'd', 'd', 'd', 'd', 'a', 'a', 'c', 'c', 'c', 'c', 'b', 'b', 'b']
Note that the letters will not necessarily be in order as shown above! But anyways, you can sort them easily using Python's built in sorted function.
>>> sum(sorted(w*[k] for k, w in dic.items()), [])
['a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd']
I have a list:
x = ['c', 'a', 'e']
I can sort this list:
x_sorted = sorted(x)
x_sorted is now ['a', 'c', 'e']
Now let's say I have a new variable y = 'd'
I want to find out where in x_sorted this new variable would fall. In this example the new variable y contains the string 'd' so it would be placed as ['a', 'c', 'd', 'e'] in the index 2 of the list. I desire to find out this index number as efficiently as possible (since I have to repeat this process many times).
Here is a function I wrote which does the task very simply:
def f(x_sorted, y):
new_list = x_sorted[:] + [y]
return sorted(new_list).index(y)
This gives me the correct answer.
I am wondering if there is a better more efficient way of doing this, as f will be called 100,000+ times.
Thanks in advance!
You can use bisect
from bisect import bisect
l = ['a', 'c', 'e']
print(bisect(l,"d"))
2
To add it to the list:
from bisect import insort
l = ['a',"b", 'c', 'e']
insort(l, "d")
print(l)
insort(l, "f")
print(l)
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e', 'f']
If you want a faster insert you could use a blist where maintaining a sorted list with insort is:
O(log**2 n) vs O(n)
from bisect import insort
from blist import blist
b = blist(["a", "b", "c", "e"])
insort(b, "f")
insort(b, "d")
print(b)
blist(['a', 'b', 'c', 'd', 'e', 'f'])
There is also a blist.sortedlist list where you can use .add:
from blist import sortedlist
l = ['b',"a", 'c', 'e']
b = sortedlist(l)
b.add("f")
print(b)
sortedlist(['a', 'b', 'c', 'e', 'f'])
There is also a sortedcontainers library that has a sortedlist implementation.
If x doesn't change, or changes infrequently, you can pre-sort it and then use binary search on the sorted list. This will incur O(n logn) cost for every sort plus O(logn) for every subsequent lookup.
If x changes a lot, you could use linear search:
>>> x = ['c', 'a', 'e']
>>> y = 'd'
>>> sum(y > el for el in x)
2
This has O(n) lookup complexity.
That is surely not the efficient way as you demonstrated in your question, In that case you are sorting it each time and hence if you perform this action m times the complexity would be O(m*n*log(m)), So the preferred way is to sort it once and then simply iterate over the list to find the index, which could be done in O(n), However the best approach would be to use Binary Search and Now your time complexity would get down to O(log(n)). Which is minimal complexity for such kind of problem.
In Python I have a list of elements aList and a list of indices myIndices. Is there any way I can retrieve all at once those items in aList having as indices the values in myIndices?
Example:
>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
I don't know any method to do it. But you could use a list comprehension:
>>> [aList[i] for i in myIndices]
Definitely use a list comprehension but here is a function that does it (there are no methods of list that do this). This is however bad use of itemgetter but just for the sake of knowledge I have posted this.
>>> from operator import itemgetter
>>> a_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> my_indices = [0, 3, 4]
>>> itemgetter(*my_indices)(a_list)
('a', 'd', 'e')
Indexing by lists can be done in numpy. Convert your base list to a numpy array and then apply another list as an index:
>>> from numpy import array
>>> array(aList)[myIndices]
array(['a', 'd', 'e'],
dtype='|S1')
If you need, convert back to a list at the end:
>>> from numpy import array
>>> a = array(aList)[myIndices]
>>> list(a)
['a', 'd', 'e']
In some cases this solution can be more convenient than list comprehension.
You could use map
map(aList.__getitem__, myIndices)
or operator.itemgetter
f = operator.itemgetter(*aList)
f(myIndices)
If you do not require a list with simultaneous access to all elements, but just wish to use all the items in the sub-list iteratively (or pass them to something that will), its more efficient to use a generator expression rather than list comprehension:
(aList[i] for i in myIndices)
Alternatively, you could go with functional approach using map and a lambda function.
>>> list(map(lambda i: aList[i], myIndices))
['a', 'd', 'e']
I wasn't happy with these solutions, so I created a Flexlist class that simply extends the list class, and allows for flexible indexing by integer, slice or index-list:
class Flexlist(list):
def __getitem__(self, keys):
if isinstance(keys, (int, slice)): return list.__getitem__(self, keys)
return [self[k] for k in keys]
Then, for your example, you could use it with:
aList = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
myIndices = [0, 3, 4]
vals = aList[myIndices]
print(vals) # ['a', 'd', 'e']