python list append方法
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循环的操作都是在更新这个对象的key和value,自始至终就这一个对象,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方法的更多相关文章
- Python 列表 append() 方法
描述 Python 列表 append() 方法用于在列表末尾追加新的对象. 语法 append() 方法语法: L.append(obj) 参数 obj -- 追加到列表末尾的对象. 返回值 该方法 ...
- Python List append()方法
append() 方法用于在列表末尾添加新的对象.Grammar: list.append(obj) 参数obj — 添加到列表末尾的对象.返回值该方法无返回值,但是会修改原来的列表.Case: al ...
- Python append()方法--list
描述 append()方法:用于向列表末尾添加新的对象. 语法 语法格式:list.append(object) 参数 object:添加到列表末尾的对象,这里的对象可以是一个元素.列表.字典或元组等 ...
- Python内置方法的时间复杂度(转)
原文:http://www.orangecube.net/python-time-complexity 本文翻译自Python Wiki本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Pyt ...
- python list列表 方法总结
深入链表(most on lists) The list data type has some more methods. Here are all of the methods of list ob ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- python内置方法
1. 简介 本指南归纳于我的几个月的博客,主题是 魔法方法 . 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是 ...
- Python内置方法的时间复杂度
转载自:http://www.orangecube.NET/Python-time-complexity 本页面涵盖了Python中若干方法的时间复杂度(或者叫"大欧"," ...
- Python列表函数&方法
Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表元素最大值 4 min(list)返 ...
随机推荐
- swift中文文档- 类型转换
未翻译完 待续(英语烂,求斧正) Type Casting 类型转换 Type casting is a way to check the type of an instance, and/or to ...
- 新浪微博客户端(11)-自定义checkBox
在最后一个欢迎界面上添加一个CheckBox. // 2.添加4个UIImageView ; i < NEW_FEATURE_NUMS; i++) { UIImageView *imageVie ...
- centos6.4.yum-lamp环境设置
首先防火墙开启mysql:3306 apache 80 and 81端口: vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tc ...
- php利用淘宝IP库获取用户ip地理位置
我们查ip的时候都是利用ip138查询的,不过那个有时候是不准确的,还不如自己引用淘宝的ip库来查询,这样准确度还高一些.不多说了,介绍一下淘宝IP地址库的使用. 淘宝IP地址库 淘宝公布了他们的IP ...
- Json数据
<title>无标题文档</title>//使用 jquery 必须的先加载 <script src="jquery-2.1.1.min.js"> ...
- 修改php.ini以达到 屏蔽错误信息
那是因为php.ini中关闭了错误显示,将错误写成了文件,这是人为设置的结果,display_errors =on就好了. 不过不显示错误倒安全点,建议调试时打开,然后提供服务时关闭. 提供一点资料给 ...
- 在rails下新建表
(文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天需要新建表,以下是建表语句 rails generate scaffold users ema ...
- [LA3026]Period
[LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...
- Boltzmann机
博客园不能上传附件,所以这里贴两张流程图吧.一个是模拟退火算法的流程图(Boltzmann机本实上就是反复退火的过程), 个是Boltzmann调整权值的过程.
- 使用JDBC获取各数据库的Meta信息——表以及对应的列
先贴代码,作为草稿: 第一个是工具类, MapUtil.java [java] view plain copy import java.util.ArrayList; import java.util ...