python: 列表、字典
来源:互联网 发布:省委宣传部 知乎 编辑:程序博客网 时间:2024/05/16 16:55
列表的赋值语句不创建拷贝。你得使用切片操作符来建立序列的拷贝。
# Filename: reference.pyprint 'Simple Assignment'shoplist = ['apple', 'mango', 'carrot', 'banana']mylist = shoplist # mylist is just another name pointing to the same object!del shoplist[0]print 'shoplist is', shoplistprint 'mylist is', mylist# notice that both shoplist and mylist both print the same list without# the 'apple' confirming that they point to the same objectprint 'Copy by making a full slice'mylist = shoplist[:] # make a copy by doing a full slicedel mylist[0] # remove first itemprint 'shoplist is', shoplistprint 'mylist is', mylist# notice that now the two lists are different
输出:$ python reference.pySimple Assignmentshoplist is ['mango', 'carrot', 'banana']mylist is ['mango', 'carrot', 'banana']Copy by making a full sliceshoplist is ['mango', 'carrot', 'banana']mylist is ['carrot', 'banana']
1. 首先给出一个字典对象a:
>>> a = {'a':'a_v','b':'b_v'}
2. 下面对字典进行遍历:
方式1:(错误方式)>>> for key in a:print %(key, a[key])SyntaxError: invalid syntax
方式2:(正确方式)
>>> for key in a:print 'key: %s, value: %s' %(key, a[key])key: a, value: a_vkey: b, value: b_v3. 解析:
注意:方式1错误是因为缺少了%s这个符号。0 0
- python 字典列表 列表字典
- Python字典、列表排序
- python: 列表、字典
- python列表,字典
- Python +字符串+列表+字典
- python列表 数组 字典
- python列表VS字典
- Python列表数组字典
- python 列表转字典
- python 列表字符串字典
- Python列表,集合,字典
- python字典列表实例
- python 列表、字典、集合
- python 字典套字典或列表
- PYTHON循环(列表,字典)
- Python:元组、列表、字典
- python:列表和字典型
- python字典,列表,元组
- linux 下引用第三方jar包 环境配置
- Git下的冲突解决
- WPF学习第十集-深入浅出话资源
- Nagios安装及配置
- HDU 1180(bfs)
- python: 列表、字典
- const 用法
- C#文件流的操作
- 同一个人
- commons各项目介绍(老帖子了,但觉得还有用,慢慢熟悉)
- Elementary OS安装打印机HP LaserJet 1020
- SGU 160 Magic Multiplying Machine(dp)
- 程序员常去的103个网站
- PHP发送QQ信息封装