This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 4 months ago.
I want to generate random ints from 4 specific numbers (1,2,5,10).
I only want the output to be one of these. How?
You can use the choice method:
random.choice([1, 2, 5 ,10])
Hope this helps
You can use random.choice like so:
import random
nums = [1, 2, 5, 10]
print(random.choice(nums)) # prints either 1, 2, 5, or 10
You can view more at the documentation: https://docs.python.org/3/library/random.html#random.choice
Related
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Understanding slicing
(38 answers)
Closed 6 months ago.
In C++, we do reverse(nums.begin() + 1, nums.end() - 2) for reversing a list or vector in ranges. So, is there something like this in Python for achieving the same?
All you need to do is use list slicing syntax:
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> l[3:6] = l[5:2:-1]
>>> l
[1, 2, 3, 6, 5, 4, 7, 8, 9, 10]
This question already has answers here:
How to force a list to a fixed size?
(7 answers)
Closed 3 years ago.
so I have an empty list:
my_list = []
I will be adding elements in an infinite loop function, one element each iteration. The list needs to be limited to the max of 10 elements.
Im looking for an effective way to add new elements, while moving old elements one index back.
Ex:
my_list = [0,1,2,3,4,5,6,7,8,9]
and after using (probably):
my_list.append('10')
I wish to see a list like this:
my_list = [1,2,3,4,5,6,7,8,9,10]
(first element was removed, rest were taken one place back)
Thanks in advance!
Looks like you want a deque:
from collections import deque
my_list = [0,1,2,3,4,5,6,7,8,9]
d = deque(my_list, maxlen=len(my_list))
d.append(10)
print(d)
# deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)
This question already has answers here:
How can I generate a list of consecutive numbers? [duplicate]
(8 answers)
Closed 4 years ago.
I'm sure python has a built in way to create an x size list where the contents are 0 through x-1, but I don't know how to do it. I've searched on Google as well as on here, I'm sure that I must've not been using the correct wording to find what I needed. Please help.
Ex: len([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) = 10
You are looking for the range builtin:
range(x) # python 2
list(range(x)) # python 3
This question already has answers here:
Pick N distinct items at random from sequence of unknown length, in only one iteration
(10 answers)
Closed 6 years ago.
For example, I need to choose three numbers from [-2,2] but with no duplicate, the following code can't accomplish this, I know I can do it by comparing the elements, but is there some elegant way to do so?
print(np.random.randint(-2,2,3))
Most general case is: choose m random numbers from range[a,b] with no duplicate.
Use the built-in random sample:
>>> import random
>>> random.sample(range(10), 5) # take 5 random elements from range(10)
[2, 4, 1, 7, 9]
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Closed 7 years ago.
I'm trying to reverse various lists, I feel my code is some what elegant, can any one make it more beautiful ?
board = [1,2,3,5]
board = [config[len(config)-1-i] for i,house in enumerate(config)]
print board
#expected output [5,3,2,1]
This should do what you want:
In [2]: board[::-1]
Out[2]: [5, 3, 2, 1]
See here : https://docs.python.org/2/library/functions.html#slice
And for a generator, see here : https://docs.python.org/2/library/itertools.html#itertools.islice
Use:
In[45]: board = [1,2,3,5]
In[46]: board.reverse()
In[47]: board
Out[47]: [5, 3, 2, 1]