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() 方法同时解读出来:

  1. >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
  2. >>> for k, v in knights.items():
  3. ... print(k, v)
  4. ...
  5. gallahad the pure
  6. robin the brave

在序列中循环时,索引位置和对应值可以使用 enumerate() 函数同时得到:

  1. >>> for i, v in enumerate(['tic', 'tac', 'toe']):
  2. ... print(i, v)
  3. ...
  4. 0 tic
  5. 1 tac
  6. 2 toe

同时循环两个或更多的序列,可以使用 zip() 整体打包:

  1. >>> questions = ['name', 'quest', 'favorite color']
  2. >>> answers = ['lancelot', 'the holy grail', 'blue']
  3. >>> for q, a in zip(questions, answers):
  4. ... print('What is your {0}? It is {1}.'.format(q, a))
  5. ...
  6. What is your name? It is lancelot.
  7. What is your quest? It is the holy grail.
  8. What is your favorite color? It is blue.

需要逆向循环序列的话,先正向定位序列,然后调用 reversed() 函数:

  1. >>> for i in reversed(range(1, 10, 2)):
  2. ... print(i)
  3. ...
  4. 9
  5. 7
  6. 5
  7. 3
  8. 1

要按排序后的顺序循环序列的话,使用 sorted() 函数,它不改动原序列,而是生成一个新的已排序的序列:

    1. >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
    2. >>> for f in sorted(set(basket)):
    3. ... print(f)
    4. ...
    5. apple
    6. banana
    7. orange
    8. pear

python字典和列表使用的要点的更多相关文章

  1. python 字典和列表嵌套用法

    python中字典和列表的使用,在数据处理中应该是最常用的,这两个熟练后基本可以应付大部分场景了.不过网上的基础教程只告诉你列表.字典是什么,如何使用,很少做组合说明. 刚好工作中采集promethe ...

  2. 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 ...

  3. python 字典,列表,集合,字符串,基础进阶

    python列表基础 首先当然是要说基础啦 列表list 1.L.append(object) -> None 在列表末尾添加单个元素,任何类型都可以,包括列表或元组等 2.L.extend(i ...

  4. python 字典、列表、字符串 之间的转换

    1.列表与字符串转换 1)列表转字符串: 将列表中的内容拼接成一个字符串 将列表中的值转成字符串 2)字符串转列表: 用eval转换 将字符串每个字符转成列表中的值 将字符串按分割成列表 2.列表与字 ...

  5. python字典和列表的高级应用

    1.将序列分解为单独的变量 1.1问题 包含n个元素的元组或列表.字符串.文件.迭代器.生成器,将它分解为n个变量 1.2方案 直接通过赋值操作 要求:变量个数要等于元素个数 当执行分解操作时,有时需 ...

  6. python字典和列表使用

    一.字典中健值为列表或字典 1 a.setdefault(key,[]).append(b)--键值是列表 2 a.setdefault(key,{}).append(b)--键值是字典 二.键值为列 ...

  7. python字典推导&&列表推导&&输出随机数

    字典推导: x = ['A', 'B', 'C', 'D'] y = ['Alice', 'Bob', 'Cecil', 'David'] print({i:j for i,j in zip(x,y) ...

  8. python字典中列表追加数据

    dict = {} for i in range(1, 6): if i not in dict: dict[i] = [] for j in range(101, 106): dict[i].app ...

  9. python字典里面列表排序

    #coding=utf8 #获取到的数据库ip,和负载数据,需要按照负载情况排序 a={u'1.8.1.14': [379, 368, 361, 358, 1363], u'9.2.4.3': [42 ...

随机推荐

  1. iOS工程如何支持64-bit

    苹果在2014年10月20号发布了一条消息:从明年的二月一号开始,提交到App Store的应用必须支持64-bit.详细消息地址为:https://developer.apple.com/news/ ...

  2. stretchableImageWithLeftCapWidth 的使用方法

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight: (NSInteger)topCa ...

  3. django 分页(2) 使用类 页码显示

    django 分页显示页码 views.py 显示11页码 ) < 起始位置 - 10总页数 else 总页数 > IF 当前页 小于 起始位置 结束页 IF 当前页 大于 IF 如果结束 ...

  4. HQL的语言

    HQL: 是Hibernate Query Language的缩写 1.HQL查询 特点: 与SQL相似,SQL中的语法基本上都可以直接使用 SQL查询的是表和表中的列而HQL查询的是对象或者对象中的 ...

  5. UILabel属性

    1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttr ...

  6. RAC和ASM环境下修改控制文件control file

    1,目前控制文件只有一个,为了安全性,增加到3个 SQL> select name from v$controlfile; NAME ------------------------------ ...

  7. F面经:painting house

    There are a row of houses, each house can be painted with three colors red, blue and green. The cost ...

  8. java系统时间的调用和格式转换

    java在java.text   java.util   java.lang包中查找 import java.util.*; import java.text.*; public class Text ...

  9. nyist 62 笨小熊

    http://acm.nyist.net/JudgeOnline/problem.php?pid=62 笨小熊 时间限制:2000 ms  |  内存限制:65535 KB 难度:2   描述 笨小熊 ...

  10. 是否可以继承String类?

    是否可以继承String类? String类是final类故不可以继承