keyValueResult = {'a': 1, 'b': 2}
sendData = [] def set_push_format(ip):
data_format = {
"endpoint": "test-endpoint",
"metric": "test-metric",
"timestamp": 54,
"step": 60,
"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
# 字典赋值给列表,构建JSON文件格式
sendData.append(data_format)
print 'sendData:' + key_value
print sendData if __name__ == "__main__":
set_push_format('192.168.137.10')
print 'final'
print sendData

  该句必须加上,不然append的全是同一个字典!

别人遇到的类似问题

问题:将数据库中查出的数据(列表中包含元组)转换为列表中字典。

原数据结构,从数据库查出:

cur = [("t1", "d1"), ("t2", "d2")]

转换后数据结构:

[{'description': 'd1', 'title': 't1'}, {'description': 'd2', 'title': 't2'}]

方法一,使用append, 出现错误结果

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

方法一运行结果:

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'}]

方法二,使用列表解析,结果正常

cur = [("a", "a1"), ("b", "b1")]
posts = []
posts = [dict(title=row[0], description=row[1]) for row in cur]
print "posts:",posts

方法二运行结果,正常

posts: [{'description': 'd1', 'title': 't1'}, {'description': 'd2', 'title': 't2'}]
采纳

方法一中,你的post_dict是一个字典对象,for循环的操作都是在更新这个对象的keyvalue,自始至终就这一个对象,append多少次都一样。

把字典对象放在循环内创建即可:

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

优先考虑列表解析,另,本例的tupel列表可以用循环解包,大致如下:

In [1]: cur = [("t1", "d1"), ("t2", "d2")]

In [2]: r = [{'description': description, 'title': title} for description, title in cur]

In [3]: r
Out[3]: [{'description': 't1', 'title': 'd1'}, {'description': 't2', 'title': 'd2'}

方法一的循环中,post_dict始终指向的是同一个对象。 在for循环中,使用匿名对象就可以了:

 
for row in cur:
posts.append({'title':row[0],'description':row[1]})

python list append方法的更多相关文章

  1. Python 列表 append() 方法

    描述 Python 列表 append() 方法用于在列表末尾追加新的对象. 语法 append() 方法语法: L.append(obj) 参数 obj -- 追加到列表末尾的对象. 返回值 该方法 ...

  2. Python List append()方法

    append() 方法用于在列表末尾添加新的对象.Grammar: list.append(obj) 参数obj — 添加到列表末尾的对象.返回值该方法无返回值,但是会修改原来的列表.Case: al ...

  3. Python append()方法--list

    描述 append()方法:用于向列表末尾添加新的对象. 语法 语法格式:list.append(object) 参数 object:添加到列表末尾的对象,这里的对象可以是一个元素.列表.字典或元组等 ...

  4. Python内置方法的时间复杂度(转)

    原文:http://www.orangecube.net/python-time-complexity 本文翻译自Python Wiki本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Pyt ...

  5. python list列表 方法总结

    深入链表(most on lists) The list data type has some more methods. Here are all of the methods of list ob ...

  6. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  7. python内置方法

    1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...

  8. Python内置方法的时间复杂度

    转载自:http://www.orangecube.NET/Python-time-complexity 本页面涵盖了Python中若干方法的时间复杂度(或者叫"大欧"," ...

  9. Python列表函数&方法

    Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 min(list)返 ...

随机推荐

  1. 转>>在同一个sql语句中如何写不同条件的count数量

    今天在做Portal中的Dashboard展现的时候,需要对多个统计字段做展现,根据我现在的掌握水平,我只能在sql调用构建器中实现一种sql语 句返回的resultSet做展现.没有办法,只能从数据 ...

  2. Java中的final修饰符

    1.什么时候可以选择final修饰符 如果想让一个类不被其他类继承,不允许在有子类,这时候就要考虑用到final来修饰. 2.用final修饰的类 首先大家要明白,用final修饰的类是不能被继承的, ...

  3. [转]Ubuntu 系统 Update-rc.d 命令

    本文转自:http://wangyan.org/blog/ubuntu-update-rc-d.html 以防止原博客挂掉,特此做了备份. Ubuntu或者Debian系统中update-rc.d命令 ...

  4. 如何下载google play免费应用的apk文件

    到这里: http://apps.evozi.com/apk-downloader/ 一看便知.

  5. 安装mac os x时about a second remaining解决方法

    转自: http://www.hongkiat.com/blog/clean-install-mavericks/ During the installation process, you may e ...

  6. 【Hadoop】HIVE 小结概览

    一.HIVE概览小结 二.HIVE安装 Hive只在一个节点上安装即可 .上传tar包 .解压 tar -zxvf hive-.tar.gz -C /cloud/ .配置mysql metastore ...

  7. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...

  8. 30.赋值运算符重载函数[Assign copy constructor]

    [问题] 给出如下CMyString的声明,要求为该类型添加赋值运算符函数.  C++ Code  1234567891011   class CMyString { public:     CMyS ...

  9. 双系统重装windows后修复UBUNTU的GRUB

    1.问题背景 本子是win7和ubuntu10.04双系统,用的还算好,虽然只有在用QQ的时候还会用到win7,但还是保留windows.可是几天 前,win7突然总是蓝屏.死机,更重要是的背景变成黑 ...

  10. javascript首尾反转字符

    var my_str="Welcome to www.sharejs.com" var i=my_str.length; i=i-1; for (var x = i; x > ...