來自使用數(shù)組(PHP)的語(yǔ)言以及只有3天的golang使用經(jīng)驗(yàn),我如何使用地圖(或切片或組合)轉(zhuǎn)換多維數(shù)組分配我在PHP中有以下代碼:$ set是文檔向量的集合(字符串=>頻率)。我通??梢詣?chuàng)建這樣的發(fā)布統(tǒng)計(jì)信息:$postinglist = array(); foreach ($set as $id=>$doc) { foreach ($doc as $term => $value) { if(!isset($postinglist[$term][$id])) { $postinglist[$term][$id] = $value; }}所以它看起來像:array ( 'the' => array ( 1 => 5, 2 => 10 ), 'and' => array ( 1 => 6, 3 => 7 ) )構(gòu)建完我的語(yǔ)料庫(kù)(所有文檔中所有術(shù)語(yǔ)的數(shù)組)之后,我將為每個(gè)術(shù)語(yǔ)構(gòu)建發(fā)布列表:$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); }}我想知道在我嘗試過的golang中是否有一種整齊的方法來做到這一點(diǎn):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 } }}當(dāng)然,這是行不通的,因?yàn)槊看挝疫@樣做時(shí),它總是會(huì)初始化地圖:postinglist[str] = make(map[int]float64)
3 回答

肥皂起泡泡
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
我可能必須這樣做:
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
}
}
- 3 回答
- 0 關(guān)注
- 550 瀏覽
添加回答
舉報(bào)
0/150
提交
取消