Keyvalueresult = {' A ': 1, ' B ': 2}senddata = []def set_push_format (IP): data_format = { "endpoint": "Test-endpoint ", " metric ":" Test-metric ", " timestamp ": Si, " step ":" Value ": 1, " CounterType ":" GAUGE ", "tags": "", } # print Keyvalueresult for key_value in Keyvalueresult: data_format = data_format.copy () # Print Key_value data_format[' endpoint '] = IP data_format[' metric '] = key_value data_format[' value '] = Keyvalueresult.get (key_value) print ' Data_format: ' + key_value print Data_format # Dictionary assigned to list, Build JSON file Format senddata.append (data_format) print ' SendData: ' + key_value print senddataif __name__ = = ' __ main__ ": set_push_format (' 192.168.137.10 ') print ' final ' print SendData
The sentence must be added, otherwise append are all the same dictionary!
Similar problems encountered by others
issue: Convert the data isolated from the database (including tuples in the list) to a dictionary in the list. The original data structure, isolated from the database:
cur = [("T1", "D1"), ("T2", "D2")]
Data structure after conversion:
[{' description ': ' D1 ', ' title ': ' T1 '}, {' Description ': ' D2 ', ' title ': ' T2 '}]
Method one, using append, error results occur
cur = [("t1", "d1"), ("t2", "d2")] post_dict = {}posts = []for row in cur: post_dict[‘title‘] = row[0] post_dict[‘description‘] = row[1] print "post_dict:",post_dict posts.append(post_dict) print "posts:",posts
Method One run Result:
post_dict: {‘description‘: ‘d1‘, ‘title‘: ‘t1‘}posts: [{‘description‘: ‘d1‘, ‘title‘: ‘t1‘}]post_dict: {‘description‘: ‘d2‘, ‘title‘: ‘t2‘}posts: [{‘description‘: ‘d2‘, ‘title‘: ‘t2‘}, {‘description‘: ‘d2‘, ‘title‘: ‘t2‘}]
Method Two, use list parsing, result normal
cur = [("a", "a1"), ("b", "b1")] posts = []posts = [dict(title=row[0], description=row[1]) for row in cur]print "posts:",posts
Method Two running result, normal
posts: [{‘description‘: ‘d1‘, ‘title‘: ‘t1‘}, {‘description‘: ‘d2‘, ‘title‘: ‘t2‘}]
Adopted
Method One, you post_dict are a Dictionary object, the operation of the for loop is to update the object's key and value , from start to finish on this object, append how many times are the same.
Place the Dictionary object inside the loop to create it:
cur = [("t1", "d1"), ("t2", "d2")] posts = []for row in cur: post_dict = {} post_dict[‘title‘] = row[0] post_dict[‘description‘] = row[1] print "post_dict:",post_dict posts.append(post_dict) print "posts:",posts
Priority is given to list parsing, and the Tupel list of this example can be wrapped in a loop, roughly as follows:
In [1]: cur = [("t1", "d1"), ("t2", "d2")]In [2]: r = [{‘description‘: description, ‘title‘: title} for description, title in cur]In [3]: rOut[3]: [{‘description‘: ‘t1‘, ‘title‘: ‘d1‘}, {‘description‘: ‘t2‘, ‘title‘: ‘d2‘}
In the loop of method one, Post_dict always points to the same object. In the For loop, you can use an anonymous object:
for row in cur: posts.append({‘title‘:row[0],‘description‘:row[1]})
Python list Append method