目前已经学习了字符串,列表和序列了。
下面的是字典类型~
01 ### 第一个程序是在Python Shell - 3.2下编译,得到的结果是正确的。###
02
03 >>> dict = {'mother':'妈妈','father':'爸爸','son':'儿子'}
04 >>> print (dict)
05 {'son': '儿子', 'father': '爸爸', 'mother': '妈妈'}
06
07 ### 第二个是在Spyder下编译,结果就不正确,怎么回事呢?###
08
09 # -*- coding: utf-8 -*-
10 """
11 Created on Tue Oct 11 22:00:43 2011
12
13 @author: guobo
14 """
15 dict = {'mother':'妈妈','father':'爸爸','son':'儿子'}
16 print dict
17
18 {'son': '\xe5\x84\xbf\xe5\xad\x90', 'father': '\xe7\x88\xb8\xe7\x88\xb8', 'mother': '\xe5\xa6\x88\xe5\xa6\x88'}
19
20 总之大家就看正确的吧,字典就那么个意思,带索引的,而且是索引可以是任意类型的。
也可以定义一个空字典
>>> type(dict1)
<class 'dict'>
>>>
>>> dict1['one'] = 'father'
>>> dict1['two'] = 'mother'
>>> dict1['three'] = 'son'
>>> print (dict1)
{'three': 'son', 'two': 'mother', 'one': 'father'}
>>>
这就是随后追加的字典元素。
字典的delete操作
>>> inventory = {'apples': 430, 'bananas': 312,'oranges': 525, 'pears': 217}
>>> print (inventory)
{'pears': 217, 'apples': 430, 'oranges': 525, 'bananas': 312}
>>> del inventory['pears']
>>> print (inventory)
{'apples': 430, 'oranges': 525, 'bananas': 312}
>>>
如果要想清空字典呢?就用clear方法
下面研究一下字典的别名和拷贝的问题
01 # -*- coding: utf-8 -*-
02 """
03 Created on Tue Oct 11 22:00:43 2011
04
05 @author: guobo
06 """
07
08 inventory_A = {'apples': 430, 'bananas': 312,'oranges': 525, 'pears': 217}
09 inventory_B = inventory_A
10 copy = inventory_A.copy()
11
12 inventory_B['apples'] = 250
13 print (inventory_A)
14 print (inventory_B)
15 print id(inventory_A)
16 print id(inventory_B)
17
18 copy['apples'] = 100
19 print (copy)
20 print id(copy)
21 print (inventory_A)
22 print (inventory_B)
23
24 {'pears': 217, 'apples': 250, 'oranges': 525, 'bananas': 312}
25 {'pears': 217, 'apples': 250, 'oranges': 525, 'bananas': 312}
26 30709632
27 30709632
28 {'oranges': 525, 'apples': 100, 'pears': 217, 'bananas': 312}
29 98204672
30 {'pears': 217, 'apples': 250, 'oranges': 525, 'bananas': 312}
31 {'pears': 217, 'apples': 250, 'oranges': 525, 'bananas': 312}
统计字符串
分享到:
相关推荐
python-3.8.0-amd64-webinstall.exe
python菜鸟3.0-终于清楚python菜鸟⼊门教程 Python是⼀款功能强⼤的脚本语⾔,具有丰富和强⼤的库,重要的是,它还具有很强的可读性,易⽤易学,⾮常适合编程初学者⼊门。以 下是⼩编为你整理的python菜鸟⼊门教程 ...
菜鸟教程python2 菜鸟教程python2 菜鸟教程python3 菜鸟教程python3 Python2.7 ⼊门指南 Python2.7 ⼊门指南 Python3.6 ⼊门指南 Python3.6 ⼊门指南 廖雪峰Python教程 廖雪峰Python教程 Python100例 Python100例 ...
python学习笔记1——(廖雪峰教程,菜鸟教程)python基础 python学习笔记1——(廖雪峰教程,菜鸟教程) >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> len(classmates) 3 ⽤索引来访问list中每⼀个位置的元素...
目录 contents 第4章pygame基础知识 第5章植物大战僵尸 第1章python菜鸟快乐游戏编程_pygame课程概述 第2章python基础环境搭建 第3章python基础知识 Python菜鸟快乐游戏编程-pygame-课件PPT模板全文共14页,当前为...
到http://www.python.org/下载最新的稳定版本Python。在Windows 平台下, 安装过程和普通的软件没有什么区别,一路下一步就可以了。
菜鸟学易语言笔记(1)-----------学会思考.pdf
runoob菜鸟教程的python3教程,用我自己写的工具下载的,非常好的书箱
python 版本: 3.9.1 windows 64位 exe安装包,官网链接:https://www.python.org/downloads/release/python-391/
一个pythone菜鸟的养成之路,对初学的人有很好的借鉴意义。
下载TX2专用torch包,Cuda版本为10.2,python版本为3.6.9,Jetpack版本为4.6,最后选择安装Pytorch v1.10.0。官网半天下不下来,难顶。
Python 实战-从菜鸟到大牛的进阶之路 - v1.1pdf
Python菜鸟教程全文共5页,当前为第1页。Python菜鸟教程全文共5页,当前为第1页。Python菜鸟教程 Python菜鸟教程全文共5页,当前为第1页。 Python菜鸟教程全文共5页,当前为第1页。 标准数据类型 Python3 中有六个...
python-3.8.0-webinstall.exe
python-note 菜鸟学习笔记
Python基础语法-菜鸟教程-函数⽤法:input()和raw_input() 1.input()和raw_input()⽤法 (1).input() 1)⽤法解释 def input(*args, **kwargs): # real signature unknown """ Read a string from standard input....
0基础学习的源码笔记(非千锋官方,本人学习时参照千锋教程写出的). 源码是在 视窗系统 xp sp3 32位,蟒蛇3.4.4 32位,pycharm社区版2016.3.6 32位环境完成的. 感谢千锋教育的开源教程 千锋教育的开源教育视频(哔哩哔哩...
pyqt5-python-Gui的入门教材,讲的非常简单易懂,建议学QT的初学者学习!