Go 在围棋中查找句子中的单词列表
在python中,我有这样的代码Go 在围棋中查找句子中的单词列表,go,Go,在python中,我有这样的代码 search_words_nonTexas = ["tx", "texas", "houston"] pass = any(word in title for word in search_words_nonTexas) 在围棋中,我一直在尝试这个 firstPass := strings.ContainsAny("title", searchWordsNonTexas) 我得到一个错误(如下所示)关于参数不正确。围棋的等价物是什么 cannot use s
search_words_nonTexas = ["tx", "texas", "houston"]
pass = any(word in title for word in search_words_nonTexas)
在围棋中,我一直在尝试这个
firstPass := strings.ContainsAny("title", searchWordsNonTexas)
我得到一个错误(如下所示)关于参数不正确。围棋的等价物是什么
cannot use searchWordsNonTexas (type [10]string) as type string in
argument to strings.ContainsAny
在python中我有代码。围棋的等价物是什么
在较低级别的语言Go中,编写自己的函数 比如说,
package main
import (
"fmt"
"strings"
"unicode"
)
// Look for list of words in a sentence.
func WordsInSentence(words []string, sentence string) []string {
var in []string
dict := make(map[string]string, len(words))
for _, word := range words {
dict[strings.ToLower(word)] = word
}
f := func(r rune) bool { return !unicode.IsLetter(r) }
for _, word := range strings.FieldsFunc(sentence, f) {
if word, ok := dict[strings.ToLower(word)]; ok {
in = append(in, word)
delete(dict, word)
}
}
return in
}
func main() {
words := []string{"tx", "texas", "houston"}
sentence := "The Texas song Yellow Rose of Texas was sung in Houston, TX."
in := WordsInSentence(words, sentence)
fmt.Println(in)
}
游乐场:
输出:
[texas houston tx]