Go中的元组分配

时间:2019-02-16 19:07:01

标签: go

使用此代码:

sns.countplot(x='new salery',data=df,palette='viridis', 
              order=['light', 'medium-', 'medium', 'medium+', 'large'])

The Go Playground的输出是:

for i := uint64(3); n > 1; i, n = i+2, n-1 {

不是正确的结果。

这段代码:

1999993

使用哪个:

func (p *Prime) Generate(n uint) {
    p.Primes = make([]uint64, 1, n)
    p.Primes[0] = 2
next:
    for i := uint64(3); n > 1; i += 2 {
        q := uint64(math.Sqrt(float64(i)))
        for _, v := range p.Primes[1:] {
            if v > q {
                break
            }
            if i%v == 0 {
                continue next
            }
        }
        p.Primes = append(p.Primes, i)
        n--
    }
}

然后

for i := uint64(3); n > 1; i += 2 {

输出正确:

n--

15485863

我在Go中的元组Assignments上丢失了某些东西吗?

谢谢。

1 个答案:

答案 0 :(得分:5)

不是,不是元组分配会导致错误的结果。

两个代码之间有细微的差别,这会导致该错误。在playgound代码中,i,n = i+2,n-1使n = n-1在每次循环迭代时运行,而github代码仅在n = n-1为质数时运行i(跳过{{1} }(如果n--运行)。