Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 266

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report