创建列表的映射?

huangapple go评论90阅读模式
英文:

Create Map of Lists?

问题

我有一个类似于这样的结构体,它包含一个任务ID、一个日期和一个注释字段:

type Journal struct {
    task_id int
    date  int
    comment string
}

对于每个任务ID,我想创建一个包含该任务ID的所有日志的列表。然后,我想将每个日志列表放入一个映射中,以便轻松管理每个任务ID。这是一些示例数据:

100, 20140701, "Failed to complete"
100, 20140702, "Removed data and finished in 5 minutes"

120, 20140701, "No issues"

130, 20140701, "Called analyst"
130, 20140702, "reloaded data"
130, 20140703, "Emailed results"

目前我不确定是否需要对注释进行排序,只需知道它们发生的日期即可。

如何创建这个映射的列表?我还没有看到过使用make创建列表的示例。

x := make(map[int][]Journal)

这里的[]Journal表示值的类型是Journal的切片,即一个日志列表。通过这种方式,你可以将每个任务ID映射到一个日志列表。

英文:

I have a struct that has an task id, a date, and a comment field similar to this:

type Journal struct {
    task_id int
    date  int
    comment string
}

For each task_id, I was to create a list of all Journals for that task_id. Then I'd like to put each Journal List into a map so that I can easily manage each of my task id's. Here is some sample data:

100, 20140701, "Failed to complete"
100, 20140702, "Removed data and finished in 5 minutes"

120, 20140701, "No issues"

130, 20140701, "Called analyst"
130, 20140702, "reloaded data"
130, 20140703, "Emailed results"

For now I'm not sure if I need to order the comments beyond knowing which date they occurred.

How do I create the map of Lists? I haven't seen any examples where I "make" a list.

x := make(map[int]Journal)

答案1

得分: 3

你可以使用以下代码创建你想要的map

var m = make(map[int][]Journal)
for _, journal := range all_my_journals {
    m[journal.task_id] = append(m[journal.task_id], journal)
}

假设all_my_journals是一个包含所有Journal变量的切片(或数组)。

然后,你可以通过给定的id获取一个包含所有Journal的切片(而不是列表):

journals := m[my_task_id]
英文:

You can create the map you want with this code:

var m = make(map[int][]Journal)
for _, journal := range all_my_journals {
    m[journal.task_id] = append(m[journal.task_id], journal)
}

Assuming all_my_journals is a slice (or array) of all the Journal variables you have.

Then, you can have a slice (and not a list) of all the Journals of a given id:

journals := m[my_task_id]

答案2

得分: 2

添加到Julienc的出色回答中,如果您的意图是能够对期刊进行排序,您可以使用一个实现了sort.Interface的结构体,就像这样

type JournalSorter struct {
    s   []*Journal
    f   func(a, b *Journal) bool
    rev bool
}

func NewSorter(j []*Journal) (js *JournalSorter) {
    js = &JournalSorter{s: make([]*Journal, len(j))}
    for i := range j {
        js.s[i] = j[i]
    }
    return
}

func (js *JournalSorter) ById(reverse bool) []*Journal {
    js.rev = reverse
    js.f = js.byId
    sort.Sort(js)
    return js.s
}

func (js *JournalSorter) ByDate(reverse bool) []*Journal {
    js.rev = reverse
    js.f = js.byDate
    sort.Sort(js)
    return js.s
}

func (js *JournalSorter) byId(a, b *Journal) bool {
    if js.rev {
        return a.Id > b.Id
    }
    return a.Id < b.Id
}

func (js *JournalSorter) byDate(a, b *Journal) bool {
    if js.rev {
        return a.Date > b.Date
    }
    return a.Date < b.Date
}

func (js *JournalSorter) Len() int {
    return len(js.s)
}

func (js *JournalSorter) Swap(i, j int) {
    if js.s != nil && js.f != nil {
        js.s[i], js.s[j] = js.s[j], js.s[i]
    }
}

func (js *JournalSorter) Less(i, j int) bool {
    if js.f != nil {
        return js.f(js.s[i], js.s[j])
    }
    return false
}
///..................

func main() {
    journals := m[my_task_id] //m = map[int][]*Journal
    s1 := NewSorter(journals)
    sorted_by_reverse_date := s1.ByDate(true)
}

这段代码定义了一个名为JournalSorter的结构体,它实现了sort.Interface接口。您可以使用NewSorter函数创建一个JournalSorter对象,并使用ByIdByDate方法对期刊进行排序。在main函数中,您可以看到如何使用这些方法对日期进行逆序排序。

英文:

Adding to Julienc's excellent answer, if your intention is to be able to sort the journals, you could use a struct that implements sort.Interface like this:

type JournalSorter struct {
s   []*Journal
f   func(a, b *Journal) bool
rev bool
}
func NewSorter(j []*Journal) (js *JournalSorter) {
js = &amp;JournalSorter{s: make([]*Journal, len(j))}
for i := range j {
js.s[i] = j[i]
}
return
}
func (js *JournalSorter) ById(reverse bool) []*Journal {
js.rev = reverse
js.f = js.byId
sort.Sort(js)
return js.s
}
func (js *JournalSorter) ByDate(reverse bool) []*Journal {
js.rev = reverse
js.f = js.byDate
sort.Sort(js)
return js.s
}
func (js *JournalSorter) byId(a, b *Journal) bool {
if js.rev {
return a.Id &gt; b.Id
}
return a.Id &lt; b.Id
}
func (js *JournalSorter) byDate(a, b *Journal) bool {
if js.rev {
return a.Date &gt; b.Date
}
return a.Date &lt; b.Date
}
func (js *JournalSorter) Len() int {
return len(js.s)
}
func (js *JournalSorter) Swap(i, j int) {
if js.s != nil &amp;&amp; js.f != nil {
js.s[i], js.s[j] = js.s[j], js.s[i]
}
}
func (js *JournalSorter) Less(i, j int) bool {
if js.f != nil {
return js.f(js.s[i], js.s[j])
}
return false
}
///.................
func main() {
journals := m[my_task_id] //m = map[int][]*Journal
s1 := NewSorter(journals)
sorted_by_reverse_date := s1.ByDate(true)
}

huangapple
  • 本文由 发表于 2014年7月6日 18:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/24594901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定