Get the App
SLTechnology News&Howtos  ›  Development  › 

How does the python list set json dictionary filter data based on the same key

Shulou Source: shulou.com Published: 2022-05-31 20:06:45 09月19日 Update

This article introduces the relevant knowledge of "python list set json Dictionary how to filter data based on the same key". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Foreword:

The following minor problems are encountered in the work, the solutions are as follows, may be more violent, temporarily keep the file, and then optimize.

Requirement: the data with the same value of the 'id' field of json in the list will be arranged in a certain priority order according to the value of type, and only the type with the highest priority will be retained in the list.

Test case:

List1 sample data:

Type priority list: [6, 4, 2, 5, 8, 3, 7, 1] (decreasing in turn, 6 has the highest priority and 1 has the lowest priority)

Draw_data = [{'geometry': {"coordinates": [121.8763583333333333, 30.865677777778]}, "properties": {' type':'8'}, "id": "03N3211"}, {'geometry': {"coordinates": [121.87635833333333, 30.86567777777778]}, "properties": {' type':'5'}, "id": "01N2234"}, {'geometry': {"coordinates": [121.876358333333] 30.86567777777778]}, "properties": {'type':'8'}, "id": "03N3211"}, {' geometry': {"coordinates": [121.87588611111111111130.8660861111112]}, "properties": {'type':'32'}, "id": "01N2234"}, {' geometry': {"coordinates": [121.87635833333333, 30.86567777777778]}, "properties": {'type':'8'} "id": "09N1111"}, {'geometry': {"coordinates": [121.8763583333333333, 30.8656777777777778]}, "properties": {' type':'11'}, "id": "03N3211"}, {'geometry': {"coordinates": [121.87635833333333, 30.86567777777778]}, "properties": {' type':'2'}, "id": "09N1111"}} {'geometry': {"coordinates": [121.87705277777778, 30.86705]}, "properties": {' type':'2'}, "id": "01N2234"}]

The above results should be:

Draw_data = [{'geometry': {"coordinates": [121.87588611111111111130.866081111112]}, "properties": {' type':'32'}, "id": "01N2234"}, {'geometry': {"coordinates": [121.87635833333333, 30.86567777777778]}, "properties": {' type':'8'}, "id": "09N1111"}, {'geometry': {"coordinates": [121.876358333333] 30.86567777777778]}, "properties": {'type':'11'}, "id": "03N3211"},] def removeduplicate (self, list1, priority=None): "" list covers dictionary to repeat Filter the highest priority type of id for the same group string: param list1: enter a list with duplicate values: priority: priority list: return: return a list without duplicates "" sort_dict = {'6percent: 100,' 4percent: 99,' 2percent: 98, '5percent: 97,' 8percent: 96, '3percent: 95,' 7percent: 94 " '1colors: 93} # self.types color table is sorted by priority newlist = [] print ("list1:", list1) for ind_i, i in enumerate (list1): # traverse the original dictionary flag = True if newlist = = []: # if the list is empty, there will be no duplicates Add pass else: for ind_j directly to it J in enumerate (newlist): j_id = j ['id'] if j_id = = I [' id']: # same id if sort_dict [j ['properties'] [' type']] = sort_dict [li ['properties'] [' type']]: Newlist [ind _ li] = I else: flag = False if flag: newlist.append (I) return newlist "how the python list set json Dictionary filters data based on the same key" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Priority same dictionary data highest content more knowledge original practical lowest successful next preface dilemma field actual situation article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Shulou Technology OPPO Reno NVidia