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. derby数据库操作比较难理解的错误及解决方法大全

    一.插入(INSERT时报错) 1.错误:java.sql.SQLIntegrityConstraintViolationException: 列“test”无法接受空值. 可能原因:建表时test列 ...

  2. C++ 中宏的使用 --来自:http://blog.csdn.net/hgl868/article/details/7058906

    宏在代码中的使用实例: g_RunLog2("Middleware client for Linux, build:%s %s", __DATE__, __TIME__); 下面详 ...

  3. OpenXML_导入Excel到数据库(转)

    (1).实现功能:通过前台选择.xlsx文件的Excel,将其文件转化为DataTable和List集合 (2).开发环境:Window7旗舰版+vs2013+Mvc4.0 (2).在使用中需要用到的 ...

  4. 保存vim的ide环境

    开发周期不是一两天, 要把当前的窗口布局, 命令历史/寄存器历史等保存下来,以便下次编写时快速恢复. 需要保存两个方面的信息: session: 保存窗口的view试图窗口布局, 和全局设置   :m ...

  5. Python ===if while for语句 以及一个小小网络爬虫实例

    if分支语句 >>> count=89 >>> if count==89: print count 89                          #单分支 ...

  6. 用 VeraCrypt 加密闪存盘

    导读 很多安全专家偏好像 VeraCrypt 这类能够用来加密闪存盘的开源软件,是因为可以获取到它的源代码.要是你需要在 Windows 系统,苹果的 OS X 系统或者 Linux 系统上加密以及访 ...

  7. unity3d web.config设置

    原地址:http://www.cnblogs.com/88999660/archive/2013/03/22/2976105.html <?xml version="1.0" ...

  8. 二叉树建立,先序、中序、后序遍历(c实现)

    建立树ABC##DE#G##F###,输出 #include <stdio.h> #include <stdlib.h> #define ElemType char //节点声 ...

  9. C# 浅谈接口的优势

    总结了一下接口的小优势,可以便于新手理解为什么要用接口,用接口有什么好处. 1.接口的定义: 关键字:interface,接口名一般大写I开头,接口中定义方法,但是不实现方法 interface IB ...

  10. 修改setup.py的源

    方法一: 修改文件 ~/.pydistutils.cfg为: [easy_install] index_url = http://pypi.douban.com/simple 方法二: 直接在setu ...