python字典和列表使用的要点
dicts = {}
lists = []
dicts['name'] = 'zhangsan'
lists.append(dicts)
这时候lists的内容应该是[{'name': 'zhangsan'}]
现在改变dicts的值
dicts['name'] = 'lisi'
因为是引用的dicts的值,所以这时候lists的内容应该是[{'name': 'lisi'}]
如果使用
if dicts not in lists:
lists.append(dicts)
这是永远不会执行的,因为lists的值引用了dicts的值,所以lists的值永远和dicts的值一样,在循环中要将dicts重新设置成一个新字典
在字典中循环时,关键字和对应的值可以使用 iteritems() 方法同时解读出来:
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
在序列中循环时,索引位置和对应值可以使用 enumerate() 函数同时得到:
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
0 tic
1 tac
2 toe
同时循环两个或更多的序列,可以使用 zip() 整体打包:
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
需要逆向循环序列的话,先正向定位序列,然后调用 reversed() 函数:
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
要按排序后的顺序循环序列的话,使用 sorted() 函数,它不改动原序列,而是生成一个新的已排序的序列:
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
python字典和列表使用的要点的更多相关文章
- python 字典和列表嵌套用法
python中字典和列表的使用,在数据处理中应该是最常用的,这两个熟练后基本可以应付大部分场景了.不过网上的基础教程只告诉你列表.字典是什么,如何使用,很少做组合说明. 刚好工作中采集promethe ...
- Python 字典和列表的对比应用
Q:将下列格式的txt文件,打印出该选手的3个最快跑步时间 james2.txt =>“James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:0 ...
- python 字典,列表,集合,字符串,基础进阶
python列表基础 首先当然是要说基础啦 列表list 1.L.append(object) -> None 在列表末尾添加单个元素,任何类型都可以,包括列表或元组等 2.L.extend(i ...
- python 字典、列表、字符串 之间的转换
1.列表与字符串转换 1)列表转字符串: 将列表中的内容拼接成一个字符串 将列表中的值转成字符串 2)字符串转列表: 用eval转换 将字符串每个字符转成列表中的值 将字符串按分割成列表 2.列表与字 ...
- python字典和列表的高级应用
1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...
- python字典和列表使用
一.字典中健值为列表或字典 1 a.setdefault(key,[]).append(b)--键值是列表 2 a.setdefault(key,{}).append(b)--键值是字典 二.键值为列 ...
- python字典推导&&列表推导&&输出随机数
字典推导: x = ['A', 'B', 'C', 'D'] y = ['Alice', 'Bob', 'Cecil', 'David'] print({i:j for i,j in zip(x,y) ...
- python字典中列表追加数据
dict = {} for i in range(1, 6): if i not in dict: dict[i] = [] for j in range(101, 106): dict[i].app ...
- python字典里面列表排序
#coding=utf8 #获取到的数据库ip,和负载数据,需要按照负载情况排序 a={u'1.8.1.14': [379, 368, 361, 358, 1363], u'9.2.4.3': [42 ...
随机推荐
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class ...
- 在Fedora8上安装jdk-7u25-linux-i586.rpm的步骤
按:我们身处一个信息爆炸的年代,当有事不决时,打开搜索引擎瞬息间就能得到海量的答案.但是,这未必会让你的问题迎刃而解,因此很多“答案”会把你引向错误的方向,浪费你的时间.希望搜索引擎能有所改进,对明确 ...
- opengl 3.3 tutorial
http://www.mbsoftworks.sk/index.php?page=tutorials&series=1
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...
- Pytho实现tail -f
实现Python版的tail -f功能 tail -f 的功能非常好用.我们用Python也可以实现这样的功能.实现的原理是通过Python版本的inotify获得文件的更新消息,从而读取更新的行.p ...
- C# MVC提交表单的四种方式(转)
Mvc 提交表单的4种方法全程详解(转) 一,MVC HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttribu ...
- 12c Grid Infrastructure Management Repository (GIMR)
1.什么是管理资料库(Management Repository) 管理资料库是12c中oracle clusterware管理的一个单实例数据库.对应的数据库名是MGMTDB.因为是一个单实例数据库 ...
- 运行sql server profiler所需的权限
********运行Sql Server Profiler所需的权限(performance)*********/ --EG. -- 使用TRACE帐户(Performancetest)跟踪Sql S ...
- Leetcode: Mini Parser
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- 使用opencv显示视频的方法
下面对使用opencv显示视频做一个简单的记录.当然,网上这方面的资料已经数不胜数了,我只是将其简单记录,总结一下. 在opencv中显示视频主要有: (1)从本地读取视频和调用摄像头读取视频 (2) ...