python颜色随机生成器

python颜色随机生成器

1. 代码:

def random_color(number=number):
    color = []
    intnum = [str(x) for x in np.arange(10)]
    #Out[138]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    alphabet =  [chr(x) for x in (np.arange(6) + ord('A'))]
    #Out[139]: ['A', 'B', 'C', 'D', 'E', 'F']
    colorArr = np.hstack((intnum, alphabet))
    #Out[142]: array(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C','D', 'E', 'F'], dtype='
    for j in range(number):
        color_single = '#'
        for i in range(6):
            index = np.random.randint(len(colorArr))
            color_single += colorArr[index]
        #Out[148]: '#EDAB33'
        color.append(color_single)
    return color
    del color, intnum, alphabet, colorArr, j, i, color_single, index, number
 
color = random_color(number=6)
#Out[150]: ['#81D4D4', '#70344F', '#DF91B1', '#7EE250', '#C47BC3', '#9F88D5'] 

2. 小记:

  1. 字符转数字 ord('a') 97
    数字转字符 chr(71) ‘G’
  2. []与()的区别
(np.arange(6) + ord('A'))
Out[158]: array([65, 66, 67, 68, 69, 70])
type((np.arange(6) + ord('A')))
Out[166]: numpy.ndarray

[np.arange(6) + ord('A')]
Out[159]: [array([65, 66, 67, 68, 69, 70])]
type([np.arange(6) + ord('A')])
Out[165]: list

你可能感兴趣的:(python)