How to generate a list of 50 random colours in python?
Question:
Given colour = [ “red”, “blue”, “green”, “yellow”, “purple”, “orange”, “white”, “black” ]
generate and print a list of 50 random colours. You will need to use the random module to get random numbers. Use range and map to generated the required amount of numbers. Then use map to translate numbers to colours. Print the result.
This is a question i’ve been given and here’s my code so far
colour = [ "red", "blue", "green", "yellow", "purple", "orange", "white", "black" ]
number=random.randint(1,9)
number.range(50)
i’m assuming this has made a variable that picks random numbers between 1-9, and then makes 50 of them ? i now need some way of linking the numbers to the colours.. i know this question is quite vague but if anyone could point me in the right direction, that would be awesome !
Answers:
You can use a simple list comprehension for this:
[ colour[random.randint(0,len(colour)-1)] for x in range(0,50) ]
colour[i]
means the i
th element in the colour
list. A random integer is created from 0 to the length of the list minus one, len(colour)-1
, with random.randint
as you suggested. This is repeated 50 times with range(1,50)
. The dummy iterator value x
in the list comprehension is just ignored.
Hope this helps!
What you need is to use random.choice(seq)
50 times passing it colour
list as argument.
Like this:
rand_colours = [random.choice(colour) for i in range(50)]
random.choice(seq)
returns randomly selected element from seq
.
For some reason, your question requires use of map
. It is difficult to help with this question without giving the answer directly, especially because these kinds of manipulations are one-liners. To start, using map and range to get a list of random numbers in the required range:
>>> nums = map(lambda x : random.randint(0,7), range(50))
>>> nums
[6, 6, 2, 4, 7, 6, 6, 7, 1, 4, 3, 2, 6, 1, 1, 2, 2, 0, 7,
3, 6, 1, 5, 2, 1, 2, 6, 0, 3, 0, 2, 6, 0, 6, 3, 5, 0, 7,
2, 5, 4, 1, 0, 0, 1, 4, 3, 3, 0, 3]
Observe that the argument to lambda, x
is not used. That is at least one reason why I wouldn’t use map here. Then, using the list of numbers, map the indexing function onto the numbers to obtain the list of colours:
>>> cols = map(lambda i: colour[i], nums)
>>> cols
['white', 'white', 'green', 'purple', 'black', 'white', 'white',
'black', 'blue', 'purple', 'yellow', 'green', 'white',
'blue', 'blue', 'green', 'green', 'red', 'black', 'yellow',
'white', 'blue', 'orange', 'green', 'blue', 'green', 'white',
'red', 'yellow', 'red', 'green', 'white', 'red', 'white',
'yellow', 'orange', 'red', 'black', 'green', 'orange', 'purple',
'blue', 'red', 'red', 'blue', 'purple', 'yellow', 'yellow', 'red',
'yellow']
The answer given by soulcheck, using random.choice()
in a list comprehension, is by far the best way of determining the answer.
If you want n random hex colors, try this:
import random
get_colors = lambda n: ["#%06x" % random.randint(0, 0xFFFFFF) for _ in range(n)]
get_colors(3) # sample return: ['#8af5da', '#fbc08c', '#b741d0']
Another solution for getting 50 HEX colors’ values:
def random_color():
return "#{}{}{}{}{}{}".format(*(random.choice("0123456789abcdef") for _ in range(6)))
colors = [random_color() for _ in range(50)]
print(*colors)
Given colour = [ “red”, “blue”, “green”, “yellow”, “purple”, “orange”, “white”, “black” ]
generate and print a list of 50 random colours. You will need to use the random module to get random numbers. Use range and map to generated the required amount of numbers. Then use map to translate numbers to colours. Print the result.
This is a question i’ve been given and here’s my code so far
colour = [ "red", "blue", "green", "yellow", "purple", "orange", "white", "black" ]
number=random.randint(1,9)
number.range(50)
i’m assuming this has made a variable that picks random numbers between 1-9, and then makes 50 of them ? i now need some way of linking the numbers to the colours.. i know this question is quite vague but if anyone could point me in the right direction, that would be awesome !
You can use a simple list comprehension for this:
[ colour[random.randint(0,len(colour)-1)] for x in range(0,50) ]
colour[i]
means the i
th element in the colour
list. A random integer is created from 0 to the length of the list minus one, len(colour)-1
, with random.randint
as you suggested. This is repeated 50 times with range(1,50)
. The dummy iterator value x
in the list comprehension is just ignored.
Hope this helps!
What you need is to use random.choice(seq)
50 times passing it colour
list as argument.
Like this:
rand_colours = [random.choice(colour) for i in range(50)]
random.choice(seq)
returns randomly selected element from seq
.
For some reason, your question requires use of map
. It is difficult to help with this question without giving the answer directly, especially because these kinds of manipulations are one-liners. To start, using map and range to get a list of random numbers in the required range:
>>> nums = map(lambda x : random.randint(0,7), range(50))
>>> nums
[6, 6, 2, 4, 7, 6, 6, 7, 1, 4, 3, 2, 6, 1, 1, 2, 2, 0, 7,
3, 6, 1, 5, 2, 1, 2, 6, 0, 3, 0, 2, 6, 0, 6, 3, 5, 0, 7,
2, 5, 4, 1, 0, 0, 1, 4, 3, 3, 0, 3]
Observe that the argument to lambda, x
is not used. That is at least one reason why I wouldn’t use map here. Then, using the list of numbers, map the indexing function onto the numbers to obtain the list of colours:
>>> cols = map(lambda i: colour[i], nums)
>>> cols
['white', 'white', 'green', 'purple', 'black', 'white', 'white',
'black', 'blue', 'purple', 'yellow', 'green', 'white',
'blue', 'blue', 'green', 'green', 'red', 'black', 'yellow',
'white', 'blue', 'orange', 'green', 'blue', 'green', 'white',
'red', 'yellow', 'red', 'green', 'white', 'red', 'white',
'yellow', 'orange', 'red', 'black', 'green', 'orange', 'purple',
'blue', 'red', 'red', 'blue', 'purple', 'yellow', 'yellow', 'red',
'yellow']
The answer given by soulcheck, using random.choice()
in a list comprehension, is by far the best way of determining the answer.
If you want n random hex colors, try this:
import random
get_colors = lambda n: ["#%06x" % random.randint(0, 0xFFFFFF) for _ in range(n)]
get_colors(3) # sample return: ['#8af5da', '#fbc08c', '#b741d0']
Another solution for getting 50 HEX colors’ values:
def random_color():
return "#{}{}{}{}{}{}".format(*(random.choice("0123456789abcdef") for _ in range(6)))
colors = [random_color() for _ in range(50)]
print(*colors)