回答

收藏

按顺序收集值,每个值都包含一个map

技术问答 技术问答 261 人阅读 | 0 人回复 | 2023-09-11

主题函数返回到遍历代码中的映射时,键没有按顺序出现。# N# `7 P( J1 G5 h- _
如何让键按顺序排列/排序地图,让键按顺序排列,值对应?/ `) q! U6 P% }$ V2 _1 {0 ?
package mainimport(    &quot;bufio&quot;    &quot;fmt&quot;    &quot;os&quot;    &quot;regexp&quot;    &quot;strings&quot;)func stringInArray(a string,list [214]string) bool{    for _,b := range list{          if b == a{              return true;         return false}func inMap(a string,l map[string]string) bool{    for k,_ := range l{          if k == a{              return true         return false}func tag(text string) map[int]map[string]string{    lexicon := make(map[string]string)    f,err := os.Open(&quot;lexicon.txt&quot;)    if err != nil{          fmt.Println(err)   }    r := bufio.NewReader(f)    line,err := r.ReadString('\n    if err == nil{          lineAsArray := strings.Split(line,&quot; &quot;)        tagsSplice := lineAsArray[1len(lineAsArray) -1)]              tagsString := strings.Join(tagsSplice,&quot; &quot;)        lexicon[lineAsArray[0]] = tagsString   }    reg,err := regexp.Compile(&quot;[^a-zA-Z0-9\\s]&quot;)    if err != nil{          fmt.Println(err)   }    text = strings.Replace(text,&quot;-&quot;,&quot; &quot;,-1)    text = reg.ReplaceAllString(text,&quot;&quot;)    matches := strings.Fields(strings.ToLower(text))    var x [2]string    x[0] = &quot;NN&quot;    x[1] = &quot;NN&quot;    toReturn := make(map[int]map[string]string)    for i,token := range matches         default to common noun            m,ok := toReturn<i>            if !ok                  m = make(map[string]string)                toReturn<i> = m                                     toReturn<i>[&quot;token&quot;] = token            toReturn<i>[&quot;tag&quot;] = &quot;NN&quot;            if i > 0                  if toReturn<i>[&quot;tag&quot;] == &quot;NN&quot; && (toReturn[i-1][&quot;token&quot;] == &quot;would&quot;)                      toReturn<i>[&quot;tag&quot;] = &quot;VB&quot;;                                             if (toReturn[i-1][&quot;token&quot;] == &quot;the&quot;) && (toReturn<i>[&quot;token&quot;] == &quot;VB&quot; || toReturn<i>[&quot;token&quot;] == &quot;VBD&quot; || toReturn<i>[&quot;token&quot;] == &quot;VBP&quot;)                      toReturn<i>[&quot;tag&quot;] = &quot;NN&quot;                                         }            //if already in lexicon            if inMap(token,lexicon)                  toReturn<i>[&quot;tag&quot;] = string(lexicon[token])                break             convert to past-participle verb if ends with `ed`            if strings.HasPrefix(toReturn<i>[&quot;tag&quot;],&quot;N&quot;) && strings.HasSuffix(token,&quot;ed&quot;)                  toReturn<i>[&quot;tag&quot;] = &quot;VBN&quot;               convert to adverb if ends with `ly`            if strings.HasPrefix(toReturn<i>[&quot;tag&quot;],&quot;N&quot;) && strings.HasSuffix(token,&quot;ly&quot;){                toReturn<i>[&quot;tag&quot;] = &quot;RB&quot;             convert to adjective if ends with `al`            if strings.HasPrefix(toReturn<i>[&quot;tag&quot;],&quot;N&quot;) && strings.HasSuffix(token,&quot;al&quot;){                toReturn<i>[&quot;tag&quot;] = &quot;JJ&quot;             convert to plural noun if ends with `s`            if toReturn<i>[&quot;tag&quot;] == &quot;NN&quot; && strings.HasSuffix(toReturn<i>[&quot;token&quot;],&quot;s&quot;)                  toReturn<i>[&quot;tag&quot;] = &quot;NNS&quot;             convert to gerund if ends with `ing`            if strings.HasPrefix(toReturn<i>[&quot;tag&quot;],&quot;N&quot;) && strings.HasSuffix(toReturn<i>[&quot;token&quot;],&quot;ing&quot;)                  toReturn<i>[&quot;tag&quot;] = &quot;VBG&quot;              stopWords := [214]string              &quot;a&quot;,                                            &quot;about&quot;,           &quot;above&quot;,           &quot;after&quot;,           &quot;again&quot;,           &quot;against&quot;,           &quot;all&quot;,           &quot;am&quot;,           &quot;among&quot;,           &quot;an&quot;,           &quot;and&quot;,           &quot;any&quot;,           &quot;are&quot;,           &quot;aren't&quot;,           &quot;arent&quot;,           &quot;as&quot;,                                            &quot;at&quot;,           &quot;be&quot;,           &quot;because&quot;,           &quot;been&quot;,           &quot;before&quot;,           &quot;being&quot;,           &quot;below&quot;,           &quot;between&quot;,           &quot;both&quot;,           &quot;but&quot;,           &quot;by&quot;,           &quot;can't&quot;,           &quot;cant&quot;,           &quot;cannot&quot;,           &quot;could&quot;,                                            &quot;couldn't&quot;,           &quot;couldnt&quot;,           &quot;did&quot;,           &quot;didn't&quot;,           &quot;didnt&quot;,           &quot;do&quot;,           &quot;does&quot;,           &quot;doesn't&quot;,           &quot;doesnt&quot;,           &quot;doing&quot;,           &quot;don't&quot;,           &quot;dont&quot;,           &quot;down&quot;,           &quot;during&quot;,           &quot;each&quot;,           &quot;few&quot;,           &quot;for&quot;,           &quot;from&quot;,           &quot;further&quot;,           &quot;had&quot;,           &quot;hadn't&quot;,           &quot;hadnt&quot;,           &quot;has&quot;,           &quot;hasn't&quot;,           &quot;have&quot;,           &quot;haven't&quot;,           &quot;havent&quot;,           &quot;having&quot;,           &quot;he&quot;,           &quot;he'd&quot;,           &quot;hed&quot;,           &quot;he'll&quot;,           &quot;he's&quot;,           &quot;hes&quot;,           &quot;her&quot;,           &quot;here&quot;,           &quot;here's&quot;,           &quot;heres&quot;,           &quot;hers&quot;,           &quot;herself&quot;,           &quot;him&quot;,           &quot;himself&quot;,           &quot;his&quot;,           &quot;how&quot;,           &quot;how's&quot;,           &quot;hows&quot;,           &quot;i&quot;,           &quot;i'd&quot;,           &quot;id&quot;,           &quot;i'll&quot;,           &quot;i'm&quot;,           &quot;im&quot;,           &quot;i've&quot;,           &quot;ive&quot;,           &quot;if&quot;,           &quot;in&quot;,           &quot;into&quot;,           &quot;is&quot;,           &quot;isn't&quot;,           &quot;isnt&quot;,           &quot;it&quot;,           &quot;it's&quot;,           &quot;its&quot;,           &quot;itself&quot;,           &quot;let's&quot;,           &quot;lets&quot;,           &quot;me&quot;,           &quot;more&quot;,           &quot;most&quot;,           &quot;mustn't&quot;,           &quot;mustnt&quot;,           &quot;my&quot;,           &quot;myself&quot;,           &quot;no&quot;,           &quot;nor&quot;,           &quot;not&quot;,           &quot;of&quot;,           &quot;off&quot;,           &quot;on&quot;,           &quot;once&quot;,           &quot;only&quot;,           &quot;or&quot;,           &quot;other&quot;,           &quot;ought&quot;,           &quot;our&quot;,           &quot;ours&quot;,           &quot;ourselves&quot;,           &quot;out&quot;,           &quot;over&quot;,           &quot;own&quot;,           &quot;same&quot;,           &quot;shan't&quot;,           &quot;shant&quot;,           &quot;she&quot;,           &quot;she'd&quot;,           &quot;shed&quot;,           &quot;she'll&quot;,           &quot;she's&quot;,           &quot;shes&quot;,           &quot;should&quot;,           &quot;shouldn't&quot;,           &quot;shouldnt&quot;,           &quot;so&quot;,           &quot;some&quot;,           &quot;such&quot;,           &quot;than&quot;,           &quot;that&quot;,           &quot;that's&quot;,           &quot;the&quot;,           &quot;their&quot;,           &quot;theirs&quot;,           &quot;them&quot;,           &quot;themselves&quot;,           &quot;then&quot;,           &quot;there&quot;,           &quot;there's&quot;,           &quot;theres&quot;,           &quot;these&quot;,           &quot;they&quot;,           &quot;they'd&quot;,           &quot;theyd&quot;,           &quot;they'll&quot;,           &quot;theyll&quot;,           &quot;they're&quot;,           &quot;theyre&quot;,           &quot;they've&quot;,           &quot;theyve&quot;,           &quot;this&quot;,           &quot;those&quot;,           &quot;through&quot;,           &quot;to&quot;,           &quot;too&quot;,           &quot;under&quot;,           &quot;until&quot;,           &quot;up&quot;,           &quot;very&quot;,           &quot;was&quot;,           &quot;wasn't&quot;,           &quot;wasnt&quot;,           &quot;we&quot;,           &quot;we'd&quot;,           &quot;we'll&quot;,           &quot;we're&quot;,           &quot;we've&quot;,           &quot;weve&quot;,           &quot;were&quot;,           &quot;weren't&quot;,           &quot;werent&quot;,           &quot;what&quot;,           &quot;what's&quot;,           &quot;when&quot;,           &quot;when's&quot;,           &quot;whens&quot;,           &quot;where&quot;,           &quot;where's&quot;,           &quot;wheres&quot;,           &quot;which&quot;,           &quot;while&quot;,           &quot;who&quot;,           &quot;who's&quot;,           &quot;whom&quot;,           &quot;why&quot;,           &quot;why's&quot;,           &quot;with&quot;,           &quot;won't&quot;,           &quot;wont&quot;,           &quot;would&quot;,           &quot;wouldn't&quot;,  
2 P0 C* n& W( T8 @4 q3 _+ M. e7 |                                    &quot;wouldnt&quot;,           &quot;yet&quot;,           &quot;you&quot;,           &quot;you'd&quot;,           &quot;youd&quot;,           &quot;you'll&quot;,           &quot;youll&quot;,           &quot;you're&quot;,           &quot;youre&quot;,           &quot;you've&quot;,           &quot;youve&quot;,           &quot;your&quot;,           &quot;yours&quot;,           &quot;yourself&quot;,                                            &quot;yourselves&quot;,         isStopWord := map[string]bool{}                  for _,sw := range stopWords              isStopWord[sw] = true;          for key,mapOne := range toReturn              //delete stop words from toReturn            if isStopWord[mapOne[&quot;token&quot;]]                  delete(toReturn,key);              }    return toReturn}func main(){     topic := tag(&quot;did. the quick,brown yet cardinal fox conveniently jump over the lazy-dog&quot;)    for k,v := range topic{          fmt.Println(&quot;key:&quot;,k,&quot;v:&quot;,v)                 
: o: o4 N4 K  E    解决方案:                                                               
$ l+ M5 K6 `6 ]# E! h# m                                                                循环遍历的使用范围map从一次迭代到下一次迭代,没有指定迭代顺序,也不能保证相同的顺序。Go 1 运行时随机地图迭代顺序,因为程序员依赖于以前实现的稳定迭代顺序。如果需要稳定的迭代顺序,则必须维护单独的数据结构来指定顺序。( F7 b1 w) Q( q& R$ G/ g; `% y/ M
这是我修改后的示例代码版本:http : //play.golang.org/p/dvqcGPYy3-' j# F% ~3 O) i* o; G
    1 H% n3 K8 F7 ?0 x
  • package mainimport  &quot;fmt&quot;    &quot;sort&quot;)func main(){     / / To create a map as input    m := make(map[int]string)    m[1] = &quot;a&quot;    m[2] = &quot;c&quot;    m[0] = &quot;b&quot;    // To store the keys in slice in sorted order    keys := make([]int,len(m))    i := 0    for k := range m              keys = k        i     sort.Ints(keys)    // To perform the opertion you want    for _,k := range keys              fmt.Println(&quot;Key:&quot;,k,&quot;Value:&quot;,m[k])  code]输出:[code]Key: 0 Value: bKey: 1 Value: aKey: 2 Value: c0 W+ V% m) I; K6 D7 ~! a
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则