计算相似数组值的数量

huangapple go评论115阅读模式
英文:

Count similar array value

问题

我正在尝试学习Go(或Golang),但似乎无法搞定。我有两个文本文件,每个文件都包含一个单词列表。我想要计算同时存在于这两个文件中的单词数量。

以下是我目前的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "bufio"
  7. )
  8. func stringInSlice(str string, list []string) bool {
  9. for _, v := range list {
  10. if v == str {
  11. return true
  12. }
  13. }
  14. return false
  15. }
  16. func main() {
  17. // 文本文件的URL
  18. var list = "https://gist.githubusercontent.com/alexcesaro/c9c47c638252e21bd82c/raw/bd031237a56ae6691145b4df5617c385dffe930d/list.txt"
  19. var url1 = "https://gist.githubusercontent.com/alexcesaro/4ebfa5a9548d053dddb2/raw/abb8525774b63f342e5173d1af89e47a7a39cd2d/file1.txt"
  20. // 创建存储数组
  21. var buffer [2000]string
  22. var bufferUrl1 [40000]string
  23. // 设置一个计数器
  24. var sibling = 0
  25. // 读取并存储文本文件
  26. wordList, err := http.Get(list)
  27. if err != nil {
  28. log.Fatalf("获取URL时出错:%v", err)
  29. }
  30. defer wordList.Body.Close()
  31. wordUrl1, err := http.Get(url1)
  32. if err != nil {
  33. log.Fatalf("获取URL时出错:%v", err)
  34. }
  35. defer wordUrl1.Body.Close()
  36. streamList := bufio.NewScanner(wordList.Body)
  37. streamUrl1 := bufio.NewScanner(wordUrl1.Body)
  38. streamList.Split(bufio.ScanLines)
  39. streamUrl1.Split(bufio.ScanLines)
  40. var i = 0;
  41. var j = 0;
  42. // 填充数组
  43. for streamList.Scan() {
  44. buffer[i] = streamList.Text()
  45. i++
  46. }
  47. for streamUrl1.Scan() {
  48. bufferUrl1[j] = streamUrl1.Text()
  49. j++
  50. }
  51. // 错误发生在这里:
  52. // 这段代码应该是要将bufferUrl1中的每个值与buffer中的值进行比较,然后增加sibling并输出"FIND"
  53. for v := range bufferUrl1{
  54. if stringInSlice(bufferUrl1, buffer) {
  55. sibling++
  56. fmt.Println("FIND")
  57. }
  58. }
  59. // 为了测试目的,这些行将打印出两个数组的内容
  60. // fmt.Println(buffer)
  61. // fmt.Println(bufferUrl1)
  62. }

但是,现在我的构建甚至无法成功。我只看到以下消息:

  1. .\hello.go:69: cannot use bufferUrl1 (type [40000]string) as type string in argument to stringInSlice
  2. .\hello.go:69: cannot use buffer (type [2000]string) as type []string in argument to stringInSlice

请帮我看看这个问题。

英文:

I'm trying to learn Go (or Golang) and can't seem to get it right. I have 2 texts files, each containing a list of words. I'm trying to count the amount of words that are present in both files.

Here is my code so far :

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "bufio"
  7. )
  8. func stringInSlice(str string, list []string) bool {
  9. for _, v := range list {
  10. if v == str {
  11. return true
  12. }
  13. }
  14. return false
  15. }
  16. func main() {
  17. // Texts URL
  18. var list = "https://gist.githubusercontent.com/alexcesaro/c9c47c638252e21bd82c/raw/bd031237a56ae6691145b4df5617c385dffe930d/list.txt"
  19. var url1 = "https://gist.githubusercontent.com/alexcesaro/4ebfa5a9548d053dddb2/raw/abb8525774b63f342e5173d1af89e47a7a39cd2d/file1.txt"
  20. //Create storing arrays
  21. var buffer [2000]string
  22. var bufferUrl1 [40000]string
  23. // Set a sibling counter
  24. var sibling = 0
  25. // Read and store text files
  26. wordList, err := http.Get(list)
  27. if err != nil {
  28. log.Fatalf("Error while getting the url : %v", err)
  29. }
  30. defer wordList.Body.Close()
  31. wordUrl1, err := http.Get(url1)
  32. if err != nil {
  33. log.Fatalf("Error while getting the url : %v", err)
  34. }
  35. defer wordUrl1.Body.Close()
  36. streamList := bufio.NewScanner(wordList.Body)
  37. streamUrl1 := bufio.NewScanner(wordUrl1.Body)
  38. streamList.Split(bufio.ScanLines)
  39. streamUrl1.Split(bufio.ScanLines)
  40. var i = 0;
  41. var j = 0;
  42. //Fill arrays with each lines
  43. for streamList.Scan() {
  44. buffer[i] = streamList.Text()
  45. i++
  46. }
  47. for streamUrl1.Scan() {
  48. bufferUrl1[j] = streamUrl1.Text()
  49. j++
  50. }
  51. //ERROR OCCURRING HERE :
  52. // This code if i'm not wrong is supposed to compare through all the range of bufferUrl1 -> bufferUrl1 values with buffer values, then increment sibling and output FIND
  53. for v := range bufferUrl1{
  54. if stringInSlice(bufferUrl1, buffer) {
  55. sibling++
  56. fmt.Println("FIND")
  57. }
  58. }
  59. // As a testing purpose thoses lines properly paste both array
  60. // fmt.Println(buffer)
  61. // fmt.Println(bufferUrl1)
  62. }

But right now, my build doesn't even succeed. I'm only greeted with this message:

  1. .\hello.go:69: cannot use bufferUrl1 (type [40000]string) as type string in argument to stringInSlice
  2. .\hello.go:69: cannot use buffer (type [2000]string) as type []string in argument to stringInSlice

答案1

得分: 1

  1. bufferUrl1是一个数组:[4000]string。你想使用vbufferUrl1中的每个字符串)。但实际上,你想使用第二个变量——第一个变量是索引,在下面的代码中被忽略了,使用了_代替。

  2. [2000]string类型与[]string不同。在Go语言中,数组和切片是不同的。请阅读Go Slices: usage and internals。我已经将两个变量声明更改为使用具有相同初始长度的切片。

需要进行的更改以进行编译:

声明:

  1. // 创建存储切片
  2. buffer := make([]string, 2000)
  3. bufferUrl1 := make([]string, 40000)

以及第69行的循环:

  1. for _, s := range bufferUrl1 {
  2. if stringInSlice(s, buffer) {
  3. sibling++
  4. fmt.Println("FIND")
  5. }
  6. }

另外,考虑使用映射而不是切片来存储buffer,以便在stringInSlice中进行更高效的查找,而不是通过列表循环。

https://play.golang.org/p/UcaSVwYcIw 是修复了下面评论中的问题的代码(你将无法从Playground进行HTTP请求)。

英文:
  1. bufferUrl1 is an array: [4000]string. You meant to use v (each
    string in bufferUrl1). But in fact, you meant to use the second
    variable—the first variable is the index which is ignored in the code
    below using _.
  2. type [2000]string is different from []string. In Go, arrays and slices are not the same. Read Go Slices: usage and internals. I've changed both variable declarations to use slices with the same initial length using make.

These are changes you need to make to compile.

Declarations:

  1. // Create storing slices
  2. buffer := make([]string, 2000)
  3. bufferUrl1 := make([]string, 40000)

and the loop on Line 69:

  1. for _, s := range bufferUrl1 {
  2. if stringInSlice(s, buffer) {
  3. sibling++
  4. fmt.Println("FIND")
  5. }
  6. }

As a side-note, consider using a map instead of a slice for buffer for more efficient lookup instead of looping through the list in stringInSlice.

https://play.golang.org/p/UcaSVwYcIw has the fix for the comments below (you won't be able to make HTTP requests from the Playground).

huangapple
  • 本文由 发表于 2016年4月11日 03:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/36534813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定