本文介绍了洗牌数组中去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试着翻译下面的Python code到去
I tried to translate the following Python code to Go
import random
list = [i for i in range(1, 25)]
random.shuffle(list)
print(list)
但发现我的围棋版本漫长而尴尬,因为没有洗牌的功能,我必须实现的接口,并转换类型。
but found my Go version lengthy and awkward because there is no shuffle function and I had to implement interfaces and convert types.
什么是我的code的惯用围棋版本?
What would be an idiomatic Go version of my code?
推荐答案
随着你的清单仅仅是个整数,从1到25,可以使用的:
As your list is just the integers from 1 to 25, you can use Perm :
list := rand.Perm(25)
for i, _ := range list {
list[i]++
}
请注意,使用由 rand.Perm
给出一个排列洗牌任何阵列的有效途径。
Note that using a permutation given by rand.Perm
is an effective way to shuffle any array.
dest := make([]int, len(src))
perm := rand.Perm(len(src))
for i, v := range perm {
dest[v] = src[i]
}
这篇关于洗牌数组中去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!