What is an appropriate data structure to model this data?

xuanbao · · 334 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<h2>Preface</h2> <p>I&#39;m simplifying a bit for sake of the question. But consider the below, where we have some weight that corresponds to the property category type and the number of days the property has been on the market. It&#39;s not a perfect table, I know. </p> <table><thead> <tr> <th>Property Category</th> <th>1</th> <th>2</th> <th>3</th> </tr> </thead><tbody> <tr> <td>Apartment</td> <td>0.8</td> <td>0.8</td> <td>0.8</td> </tr> <tr> <td>Condo</td> <td>2.1</td> <td>2.5</td> <td>2.7</td> </tr> <tr> <td>Townhome</td> <td>1.6</td> <td>1.8</td> <td>2.0</td> </tr> </tbody></table> <p>In Javascript I would use map and reduce functions, in Golang I believe a simple <code>for</code> loop is both faster and preferred.</p> <h2>Question</h2> <p>What data structure would I use to express the above table? The property category will be the key which narrows this down to a single row. After that, I&#39;ll use the number of days this property has been on the market and look up the corresponding column. Coordinates, really. </p> <p>My first instinct is to use a string map, where the keys are the property categories (i.e. &#34;TOWNHOME&#34;) and the values are integer maps where that key is the number of days on the market and the value is the weight. </p> <p>Thoughts?</p> <hr/>**评论:**<br/><br/>IndianAlien: <pre><p>Struct propertyCategorye with 3 ints. Then a map of the structs with the property type.</p></pre>no_coats: <pre><p>It never dawned on me, not even for a second, to use structs :)</p></pre>jmatosp: <pre><p>A little over-engineering never killed nobody:</p> <p>Expressive structs: <a href="https://play.golang.org/p/Ph-GQLG7CR" rel="nofollow">https://play.golang.org/p/Ph-GQLG7CR</a></p> <p>and less over-engineered, and still expressive: <a href="https://play.golang.org/p/HNwfpilUws" rel="nofollow">https://play.golang.org/p/HNwfpilUws</a></p></pre>metamatic: <pre><p>I assume that the number of days on the market will be a monotonically increasing integer with a fairly small upper bound, and that values won&#39;t be sparse, so you might as well just use an array of floats rather than a map.</p> <p>And unless the list of categories needs to be arbitrarily extensible at run time, I&#39;d use consts for those rather than have to do O(n) string comparisons or use a map for every lookup.</p> <p>Of course, you should have a lookup function rather than allowing direct access, so that you can change the implementation later, and can check for bounds errors. For example, if you do need an arbitrarily extensible dataset, you could move the data into a relational database and still provide the same Weight API function.</p> <p>So my (probably overengineered) possible solution: <a href="https://play.golang.org/p/Mii12TrV4b" rel="nofollow">https://play.golang.org/p/Mii12TrV4b</a></p></pre>dashausSP: <pre><p>I always prefer &#34;expressive structs&#34; whenever possible and to avoid map by issue of performances.</p></pre>Andrey_Kolmogorov: <pre><p>What about a map[string][]float64?</p> <p><a href="https://play.golang.org/p/BKTpIc6REZ" rel="nofollow">https://play.golang.org/p/BKTpIc6REZ</a></p> <p>Or if the number is fixed-sized </p> <p><a href="https://play.golang.org/p/FUrv_7uxtY" rel="nofollow">https://play.golang.org/p/FUrv_7uxtY</a></p></pre>tgulacsi: <pre><p>map[string]map[int]float64 is the simplest; if the inner map is dense, then a properly sized []int could be used, too.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

334 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传