如何在Go中检查多个数组中的变量

时间:2018-01-19 03:14:38

标签: arrays go

我已经查找了几种使用Go语言查看数组的方法,但是我找不到多个搜索多个数组的方法。我的程序的目的是获取输入并搜索多个数组以查看该输入是否在其中一个数组中,如果该术语在其中一个数组中,则指定该术语所在的数组。这是我的代码到目前为止:

package main

import "fmt"

func main() {
    arr := []string{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"}
    numbers := arr[26:36]
    letters := arr[0:26]
    fmt.Println(letters)
    vowels := []string{arr[0],arr[4],arr[8],arr[14],arr[20]}
    fmt.Println(arr)
    fmt.Println(numbers)
    fmt.Println(vowels)
    consonants := []string{arr[1],arr[2],arr[3],arr[5],arr[6],arr[7],arr[9],arr[10],arr[11],arr[12],arr[13],arr[15],arr[16],arr[17],arr[18],arr[19],arr[21],arr[22],arr[23],arr[24],arr[25]}
    fmt.Println(consonants)
    odds := []string{numbers[1],numbers[3],numbers[5],numbers[7],numbers[9]}
    fmt.Println(odds)
    evens := []string{numbers[0],numbers[2],numbers[4],numbers[6],numbers[8]}
    fmt.Println(evens)
}

如果我现在通过命令提示符运行程序,那么这将是输出:

C:\Users\Owner\Desktop>go run Array.go
[a b c d e f g h i j k l m n o p q r s t u v w x y z]
[a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[a e i o u]
[b c d f g h j k l m n p q r s t v w x y z]
[1 3 5 7 9]
[0 2 4 6 8]

总而言之,我需要接受输入,并在上面的数组中搜索该术语可能存在的位置。这就是我希望示例代码吐出的内容:

Enter term: a
The term "a" was found in array "vowels"

任何和所有帮助都可以!

注意:我一直提到的术语只是一个单数字母,或0-9之间的数字。如果我的代码和/或输出看起来很草率,我也很抱歉,因为我还没来得及清理它。

3 个答案:

答案 0 :(得分:2)

使用带有键的地图作为术语和值作为术语类型的切片:

m := map[string][]string{}
for _, v := range letters {
    m[v] = append(m[v], "letters")
}
for _, v := range vowels {
    m[v] = append(m[v], "vowels")
}
for _, v := range consonants {
    m[v] = append(m[v], "consonants")
}
for _, v := range numbers {
    m[v] = append(m[v], "numbers")
}
for _, v := range odds {
    m[v] = append(m[v], "odds")
}
for _, v := range evens {
    m[v] = append(m[v], "evens")
}

fmt.Println(m["a"]) // prints [letters vowels]
fmt.Println(m["1"]) // prints [numbers odds]

Run it on the playground

答案 1 :(得分:1)

以下是Go中使用数组进行ASCII字符类测试的示例。

charclass.go

package main

import (
    "bufio"
    "fmt"
    "os"
)

const maxASCII = 0x7F

var letters = [maxASCII + 1]bool{
    'a': true, 'b': true, 'c': true, 'd': true, 'e': true, 'f': true, 'g': true,
    'h': true, 'i': true, 'j': true, 'k': true, 'l': true, 'm': true, 'n': true,
    'o': true, 'p': true, 'q': true, 'r': true, 's': true, 't': true, 'u': true,
    'v': true, 'w': true, 'x': true, 'y': true, 'z': true,
}

func isLetter(r rune) bool {
    return (r >= 0 && int(r) < len(letters)) && letters[r|0x20]
}

var vowels = [maxASCII + 1]bool{
    'a': true, 'e': true, 'i': true, 'o': true, 'u': true,
}

func isVowel(r rune) bool {
    return (r >= 0 && int(r) < len(vowels)) && vowels[r|0x20]
}

func isConsonant(r rune) bool {
    return isLetter(r) && !isVowel(r)
}

var numbers = [maxASCII + 1]bool{
    '0': true, '1': true, '2': true, '3': true, '4': true,
    '5': true, '6': true, '7': true, '8': true, '9': true,
}

func isNumber(r rune) bool {
    return (r >= 0 && int(r) < len(numbers)) && numbers[r]
}

func isOddNumber(r rune) bool {
    return isNumber(r) && (r-'0')&1 == 1
}

func isEvenNumber(r rune) bool {
    return isNumber(r) && (r-'0')&1 == 0
}

func main() {
    var classes = []struct {
        name string
        test func(rune) bool
    }{
        {"is a letter", isLetter},
        {"is a consonant", isConsonant},
        {"is a vowel", isVowel},
        {"is a number", isNumber},
        {"is an odd number", isOddNumber},
        {"is an even number", isEvenNumber},
    }

    enter := "Enter a line:"
    fmt.Println(enter)
    s := bufio.NewScanner(os.Stdin)
    for s.Scan() {
        line := s.Text()
        if len(line) == 0 {
            break
        }
        for _, r := range line {
            for _, class := range classes {
                if class.test(r) {
                    fmt.Printf("%q %s\n", r, class.name)
                }
            }
        }
        fmt.Println(enter)
    }
    if err := s.Err(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
}

输出:

$ go run charclass.go
Enter a line:
QWE asd 12 (*)
'Q' is a letter
'Q' is a consonant
'W' is a letter
'W' is a consonant
'E' is a letter
'E' is a vowel
'a' is a letter
'a' is a vowel
's' is a letter
's' is a consonant
'd' is a letter
'd' is a consonant
'1' is a number
'1' is an odd number
'2' is a number
'2' is an even number
Enter a line:

$

答案 2 :(得分:0)

使用以下地图和功能搜索关键字类别,例如&#34; GO-Play&#34;:

package main

import "fmt"

var m  = map[string][]string{}

func FindLetterType(search_key string, search_in map[string][]string) (string) {
  return_type := "";
  for type1, keys:= range search_in {
      for _, key:= range keys{
         if(key == search_key){
          return_type = return_type  + "," + type1;
        }
      }
  }
  return return_type ;
}

func main() {
    arr := []string{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"}
    numbers := arr[26:36]
    letters := arr[0:26]
    fmt.Println(letters)
    vowels := []string{arr[0],arr[4],arr[8],arr[14],arr[20]}
    fmt.Println(arr)
    fmt.Println(numbers)
    fmt.Println(vowels)
    consonants := []string{arr[1],arr[2],arr[3],arr[5],arr[6],arr[7],arr[9],arr[10],arr[11],arr[12],arr[13],arr[15],arr[16],arr[17],arr[18],arr[19],arr[21],arr[22],arr[23],arr[24],arr[25]}
    fmt.Println(consonants)
    odds := []string{numbers[1],numbers[3],numbers[5],numbers[7],numbers[9]}
    fmt.Println(odds)
    evens := []string{numbers[0],numbers[2],numbers[4],numbers[6],numbers[8]}
    fmt.Println(evens)

for _, v := range letters {
    m["letters"] = append(m["letters"], v)
}
for _, v := range vowels {
    m["vowels"] = append(m["vowels"], v)
}
for _, v := range consonants {
   m["consonants"] = append(m["consonants"], v)
}
for _, v := range numbers {
    m["numbers"] = append(m["numbers"], v)
}
for _, v := range odds {
    m["odds"] = append(m["odds"], v)
}
for _, v := range evens {
   m["evens"] = append(m["evens"], v)
}
//fmt.Println(m);

fmt.Println("a under => ", FindLetterType("a", m));
fmt.Println("a under => ", FindLetterType("1", m));
fmt.Println("a under => ", FindLetterType("2", m));
fmt.Println("a under => ", FindLetterType("b", m));


}