Reputation: 3423
Although it looks terrible, I am not finding a nicer/more efficient way of doing this:
ae = np.arange(0.0,1,0.05)
aee = np.arange(0.3,1.01,0.345)
aef = np.arange(0.3,1.01,0.345)
random.shuffle(ae)
random.shuffle(aee)
random.shuffle(aef)
for item_a in aee:
for item_b in ae:
for item_c in aef:
rlist.append(colorsys.hsv_to_rgb(item_b,item_a,item_c))
Ideas?
Upvotes: 2
Views: 284
Reputation: 26767
Stupid oneliner:
rlist = [colorsys.hsv_to_rgb(b, a, c) for c in random.sample(aef,len(aef))
for b in random.sample(ae,len(ae))
for a in random.sample(aee,len(aee))]
random.sample(x,len(x))
(new in 2.3) is pretty much equivalent to random.shuffle(x)
, but it returns a randomized copy of the list instead of None
.
This is probably much slower than shuffling or whatever else, and you don't keep a copy of the randomized lists (if you care).
Upvotes: 1
Reputation: 5565
import numpy as np
import random
import itertools
import colorsys
hue, saturation, value = np.arange(0.0,1,0.05), np.arange(0.3,1.01,0.345), np.arange(0.3,1.01,0.345)
rlist= [colorsys.hsv_to_rgb(hue, saturation, value) for hue, saturation, value in
itertools.product(random.sample(hue,len(hue)), random.sample(saturation, len(saturation)), random.sample(value, len(value)))]
print rlist
EDIT: random.sample from full population to avoid inplace separate shuffles
The version without itertools:
# without itertools
import numpy as np
import random
from pprint import pprint
import colorsys
hues, saturations, values = np.arange(0.0,1,0.05), np.arange(0.3,1.01,0.345), np.arange(0.3,1.01,0.345)
rlist= [colorsys.hsv_to_rgb(hue, saturation, value)
for hue in random.sample(hues,len(hues))
for saturation in random.sample(saturations, len(saturations))
for value in random.sample(values, len(values))]
pprint(rlist)
You can also include the definition of itertools.product from documentation (I did that in module called it.py in my server and used it instead of itertools):
product = None
from itertools import *
if not product:
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
I use itertools normally as:
import itertools as it
But in the server it is replaced by
import it
Upvotes: 4
Reputation: 70089
you don't need to shuffle each list in the first place because you will do a Cartesian product ... ;
import itertools
import colorsys
hsv_iter = itertools.product(np.arange((0, 1, 0.05),
np.arange((0.3,1.01,0.345),
np.arange((0.3,1.01,0.345))
rlist = [colorsys.hsv_to_rgb(hue, lightness, saturation)
for hue, lightness, saturation in hsv_ite]
# you can shuffle now the list if you want
random.shuffle(rlist)
Upvotes: 1
Reputation: 213115
If you do not want to shuffle the rlist
, but the initial lists, then you can try to put the last four lines into a list comprehension:
rlist = [ colorsys.hsv_to_rgb(b, a, c) for c in aef for b in ae for a in aee ]
Upvotes: 4