How to categorize list of objects based on a parameter

我们两清 提交于 2021-02-11 14:32:46

问题


I have a list of objects with this definition:

type MyObject struct {
    ID        int `json:"id"`
    Level     int `json:"level"`
    CityID    int `json:"city_id"`
}

I want to categorize them based on CityID in order to get a list of lists where each inner list's items have the same CityID.

For example, if I have the following list:

[
    MyObject {ID: 1, Level: 12, CityID: 7},
    MyObject {ID: 2, Level: 15, CityID: 2},
    MyObject {ID: 3, Level: 55, CityID: 4},
    MyObject {ID: 4, Level: 88, CityID: 7},
    MyObject {ID: 5, Level: 11, CityID: 4},
    MyObject {ID: 6, Level: 5, CityID: 7},
    MyObject {ID: 7, Level: 42, CityID: 2}
]

I need below output:

[
    [MyObject {ID: 1, Level: 12, CityID: 7}, MyObject {ID: 4, Level: 88, CityID: 7}, MyObject {ID: 6, Level: 5, CityID: 7}],
    [MyObject {ID: 2, Level: 15, CityID: 2}, MyObject {ID: 7, Level: 42, CityID: 2}],
    [MyObject {ID: 3, Level: 55, CityID: 4}, MyObject {ID: 5, Level: 11, CityID: 4}]
]

I know it is possible in python using itertools, but I'm new in go and have little knowledge about its libraries. Any help?

EDIT 1:

I am currently using this:

m := make(map[int][]MyObject)

for _, item := range myList {
    if val, ok := m[item.CityID]; ok {
        m[item.CityID] = append(val, item)
    } else {
        m[item.CityID] = []MyObject{item, }
    }
}

回答1:


Your current approach is the way to go, but it may be simplified, there is no need to check if a CityID is already in the map, because indexing a map with a key that isn't in it will result in the zero value of the value type, which is nil for slices, and you may append to a nil slice without a problem:

m := make(map[int][]MyObject)

for _, item := range myList {
    m[item.CityID] = append(m[item.CityID], item)
}



回答2:


Complete programme for you

It might be lengthy but working fine, may help if you don't have any other option.

 package main

// only needed below for sample processing

type MyObject struct {
    ID        int `json:"id"`
    Level     int `json:"level"`
    CityID    int `json:"city_id"`
}

var listOfID = make([]int, 0)

func main() {
    var listMap = make(map[int][]MyObject)

    obj :=  MyObject {ID: 1, Level: 12, CityID: 7}

    for i := 0; i < 10; i++ {
        listMap = addToList(obj, listMap)
    }

    obj1 := MyObject {ID: 1, Level: 12, CityID: 8}

    for i := 0; i < 10; i++ {
        listMap = addToList(obj1, listMap)
    }

    listOfList := getListOfLists(listMap)

    printListOfList(listOfList)
}

func printListOfList(list interface{}) {
    // print here
    return
}

func getListOfLists(listMap map[int][]MyObject) interface{} {
    var listOfLists [][]MyObject
    for i:=0; i < len(listOfID); i++ {
        innerList := listMap[listOfID[i]]
        listOfLists = append(listOfLists, innerList)
    }
    return listOfLists
}

func addToList(obj MyObject, listMap map[int][]MyObject) map[int][]MyObject {
    cityID := obj.CityID
    list := listMap[cityID]

    if list != nil {
        list = append(list, obj)
        listMap[cityID] = list
    } else {
        var newList []MyObject
        listOfID = append(listOfID, cityID)
        newList = make([]MyObject, 0)
        newList = append(newList, obj)
        listMap[cityID] = newList
    }

    return listMap
}


来源:https://stackoverflow.com/questions/59734153/how-to-categorize-list-of-objects-based-on-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!