对于我的聚类gui,我目前正在使用随机颜色进行聚类,因为我不知道手头会有多少个聚类.
在Python中,这看起来像:
import random
def randomColor():
return (random.random(),random.random(),random.random())
Run Code Online (Sandbox Code Playgroud)
但是,当我更新东西时,颜色会发生变化.
所以我倾向于拥有一个具有输入参数I的函数,例如
def nonrandomColor(i):
...
return color
Run Code Online (Sandbox Code Playgroud)
总是会为同一个I返回相同的颜色,同时保持生成任意多种颜色的能力.
答案不一定要用Python来表达,它更像是我感兴趣的总体布局.
One way is to use caching. Use a defaultdict:
>>> import random
>>> def randomColor():
... return (random.random(),random.random(),random.random())
...
>>> from collections import defaultdict
>>> colors = defaultdict(randomColor)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)
>>> colors[1]
(0.48991106537516382, 0.77039712435566876, 0.73707003166893892)
>>> colors[3]
(0.10726172906719755, 0.97327604757295705, 0.58935794305308264)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2299 次 |
| 最近记录: |