伪码算法:
用于random.choice从元素列表中随机选取元素.
这是Python代码的概念验证:
import random
sources = ['a', 'b', 'c', 'd'] # you said 4 strings
result = [random.choice(sources)]
while len(result) < 16: # you said you need 16 elements
elem = random.choice(sources)
if elem != result[-1]:
result.append(elem)
Run Code Online (Sandbox Code Playgroud)
此代码针对清晰度进行了优化,而不是简洁,聪明或速度.
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |