dtjzpg5313
2018-07-29 11:12
浏览 234
已采纳

Golang中的多维数组

Coming from a language that uses array (PHP) and only 3 days of experience in golang, how can I translate a multi-dimensional array assignment using map (or slice, or combination)

I have this code in PHP: $set is a collection of document vectors (string => frequency).

I normally can create a posting statistics like this:

$postinglist = array();  

foreach ($set as $id=>$doc) {
      foreach ($doc as $term => $value) {

      if(!isset($postinglist[$term][$id])) {
          $postinglist[$term][$id] = $value;
      }
}

So it would look something like:

array ( 
   'the' => array ( 
      1 => 5, 
      2 => 10 
      ), 
   'and' => array ( 
      1 => 6, 
      3 => 7
      )
    )

After building my corpus (just array of all terms in all documents), I would then build the posting list for each terms:

$terms = $this->getAllTerms();

foreach($terms as $term) {
    $entry = new EntryStatistics();

    foreach($postinglist[$term] as $id => $value) {
       $post = new PostingStatistics;
       $post->setTf($value);
       $entry->setPostingList($id, $post);
    }
}

I'm wondering if there's a neat way of doing such in golang as I've tried this:

postinglist := make(map[string]map[int]float64)
for id, doc := range indexmanager.GetDocuments() {
  for str, tf := range doc {
      _, ok_pl := postinglist[str][id]
      if !ok_pl {
          postinglist[str] = make(map[int]float64)
          postinglist[str][id] = tf
      }
   }
}

Of course it doesn't work, as it always initializes the map everytime I do:

postinglist[str] = make(map[int]float64)
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushanlv5184 2018-07-29 12:28
    已采纳

    Make a map if the map is nil. For example,

    package main
    
    import (
        "fmt"
        "math"
    )
    
    func main() {
        tests := []struct {
            s string
            i int
            f float64
        }{
            {"s", 42, math.Pi},
            {"s", 100, math.E},
            {"s", 100, 1000.0},
            {"x", 1, 2.0},
        }
    
        var m map[string]map[int]float64
        fmt.Println(m)
        for _, t := range tests {
            if m == nil {
                m = make(map[string]map[int]float64)
            }
            if m[t.s] == nil {
                m[t.s] = make(map[int]float64)
            }
            m[t.s][t.i] += t.f
            fmt.Println(m)
        }
    }
    

    Playground: https://play.golang.org/p/IBZxGgAi6eL

    Output:

    map[]
    map[s:map[42:3.141592653589793]]
    map[s:map[42:3.141592653589793 100:2.718281828459045]]
    map[s:map[42:3.141592653589793 100:1002.718281828459]]
    map[s:map[42:3.141592653589793 100:1002.718281828459] x:map[1:2]]
    
    已采纳该答案
    打赏 评论
  • duanjianxiu9400 2018-07-29 12:16

    I may have to do it like this:

            v_pl, ok_pl := postinglist[str]
            if !ok_pl {
                _, ok_pl2 := v_pl[id]
                if !ok_pl2 {
                    postinglist[str] = map[int]float64{id: tf}
                }
            } else {
                _, ok_pl2 := v_pl[id]
                if !ok_pl2 {
                    postinglist[str][id] = tf
                }
            }
    
    打赏 评论
  • doudun1934 2018-07-29 13:30

    If you don't use the zero value in the map, you can take advantage of comparing with nil, as peterSO suggested. In addition, I would encourage you to use custom types, e.g.

    type PostStat map[int]float64
    type PostList map[string]PostStat
    
    pl := make(PostList)
    for id, doc := range indexmanager.GetDocuments() {
        for str, tf := range doc {
            if pl[str] == nil {
                pl[str] = make(PostStat)
            }
            if _, ok := pl[str][id]; !ok {
                pl[str][id] = tf
            }
        }
    }
    

    The code above looks a bit clearer and easier to understand, in my opinion.

    打赏 评论

相关推荐 更多相似问题