dpztth71739 2015-03-03 11:53
浏览 50
已采纳

在运行时填充具有长度设置的切片时,我是否绝对需要两个循环,一个用于确定长度,一个用于填充?

So I have a function that removes punctuation from a string and puts those punctuation characters and their index into two slices:

func removeAndIndexPunctuation(word string) (string, []rune, []int) {
    // Index punctuation
    numberOfPunct := 0
    for _, char := range word {
        if unicode.IsPunct(char) {
            numberOfPunct += 1
        }
    }

    punctuations := make([]rune, numberOfPunct)
    punctuationIndex := make([]int, numberOfPunct)

    x := 0
    for i, char := range word {
        if unicode.IsPunct(char) {
            punctuations[x] = char
            punctuationIndex[x] = i
            x += 1
        }
    }

    // Remove all punctuation from word string
    res := r.ReplaceAllString(word, "")
    return res, punctuations, punctuationIndex
}

In order to make and populate the slices I have to run two for loops, one for counting the number of punctuations so I can make the array the correct length and then another that's pretty much the same except now I populate the slices.

In Python though I don't need two for loops since Python supports "dynamic arrays":

def removeAndIndexPunctuation(word):
    punctuations = []
    # Index punctuation
    for i, char in enumerate(word):
        if char in string.punctuation:
            punctuations.append((char, i))
    # Remove all punctuation from word string
    word = word.encode("utf-8").translate(None, string.punctuation).decode("utf-8")
    return word, punctuations

So I just want to make sure, in this case in golang, do I absolutely need two for loops because it doesn't support dynamic arrays or am I missing something? Or in other words, If I'm looping over a set of characters and adding some to an array/slice, do I really need two loops, one for counting the number of characters for setting the length of the slice, and one for populating the slice?

I come from Python and am learning Go.

  • 写回答

1条回答 默认 最新

  • douliang4858 2015-03-03 12:09
    关注

    You don't. Golang slices are dynamic arrays (don't confuse them for the actual arrays). You should (re-)read the excellent golang slice internals blog post about it.

    Here is your example rewritten in a more idiomatic way:

    func removeAndIndexPunctuation(word string) (string, []rune, []int) {
        var punctuations []rune
        var indexes []int
    
        for i, char := range word {
            if unicode.IsPunct(char) {
                punctuations = append(punctuations, char)
                indexes = append(indexes, i)
            }
        }
    
        // Remove all punctuation from word string
        res := r.ReplaceAllString(word, "")
        return res, punctuations, indexes
    }
    

    Note that I don't think the use of regex is particularly relevant here. Here is another version using a slice of runes:

    func removeAndIndexPunctuation(word string) (string, []rune, []int) {
        var punctuations []rune
        var indexes []int
        var result []rune
    
        for i, char := range word {
            if unicode.IsPunct(char) {
                punctuations = append(punctuations, char)
                indexes = append(indexes, i)
            } else {
                result = append(result, char)
            }
        }
    
        return string(result), punctuations, indexes
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件