映像和集合类型
来源:互联网 发布:linux中help的用法 编辑:程序博客网 时间:2024/05/12 22:27
一、映射类型:字典 1、字典的创建和赋值 PythonCode 1
2
3
2、从2.3版本后可以使用内建方法fromkeys()来创建默认的字典(字典中元素具有相同的值,如果没给出,默认为None) PythonCode 1
2
3
4
5
6
***更聪明的办法 PythonCode 1
2
3
4
5
3、访问字典的值(下面为两种访问方法) PythonCode 1
2
3
4
5
6
7
8
9
10
1>要得到字典中某个元素的值,如下: Python Code 1
2
4、字典的更新 Python Code 1
2
3
4
5
6
5、字典的删除和元素的删除 Python Code 1
2
3
4
5
6
7
8
***也可以使用:dict.clear()来进行删除字典里面的所有元素
1、dict.items()返回一个包含字典中键、值对元组的列表 PythonCode 1
2
3
2、dict.keys() 返回一个包含字典中键的列表 PythonCode 1
2
3
4
3、dict.values() 返回一个包含字典中所有值的列表 PythonCode 1
2
4、dict.update(dict2) 将字dict2的键-值对添加到字典dict中 PythonCode 1
2
3
4
5、dict.copy()用一个变量接收,返回一个拷贝过来的dict字典 Python Code 1
2
3
4
5
说明:1>在字典中,不允许一个键对应多个值(如果出现,不会报错但是会以从左往右的顺序将最后一个值给那个键) 2>在字典中所有的键必须是可哈希的(所有的不可变类型都是可哈希的) 说明:set()和frozenset()工厂函数分别用来生成可变和不可变的集合 1、创建集合类型并赋值、访问、更新、删除 PythonCode 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2、集合类型的操作符 1>联合(|) 2>交集(&) 3>差补/相对补集(-) 4>对称差分(^) PythonCode 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3、内建函数 1>add(obj) 在集合中添加一个对象obj 2>remove(obj) 从集合中删除对象obj 3>discard(obj) 如果obj是集合中的元素,从集合中删除这个对象 4>pop()删除集合中的任意一个对象,并返回它 5>clear()删除集合中的所有元素
2
3
>>> dict1 = {'name': 'earth', 'port': 80}
>>>dict1
{'name':'earth', 'port': 80}
>>>
{'name':
2
3
4
5
6
>>> ddict = {}.fromkeys(('x', 'y'), -1)
>>>ddict
{'y':-1, 'x': -1}
>>>ddict = {}.fromkeys(('x', 'y'))
>>>ddict
{'y':None, 'x': None}
>>>
{'y':
>>>
>>>
{'y':
2
3
4
5
>>> dict1 = ddict.copy()
>>>dict1
{'y':None, 'x': None}
>>>ddict
{'y':None, 'x': None}
>>>
{'y':
>>>
{'y':
2
3
4
5
6
7
8
9
10
>>> for key in dict1.keys():
... print 'key = %s, value = %s' %(key, dict1[key])
...
key= name, value = earth
key= port, value = 80
>>>for key in dic1t:
... print 'key = %s, value = %s' %(key, dict1[key])
...
key= name, value = earth
key= port, value = 80
...
...
key
key
>>>
...
...
key
key
2
>>> dict1['name']
'earth'
'earth'
2
3
4
5
6
>>> dict1
{'name':'earth', 'port': 80}
>>>dict1['name'] = 'venus'
>>>dict1['port'] = 666
>>>dict1
{'name':'venus', 'port': 666}
{'name':
>>>
>>>
>>>
{'name':
2
3
4
5
6
7
8
>>> dict1
{'name':'venus', 'port': 666}
>>>del dict1['name']
>>>dict1
{'port':666}
>>>del dict
>>>dict1
<<spanstyle="color:#8000ff;">type'dict'>
{'name':
>>>
>>>
{'port':
>>>
>>>
<<spanstyle="color:#8000ff;">type
二、映射类型内建方法
2
3
>>> dict1 = {'name': 'hellow', 'key': 110}
>>>dict1.items()
[('name','hellow'), ('key', 110)]
>>>
[('name',
2
3
4
>>> dict1
{'name':'hellow', 'key': 110}
>>>dict1.keys()
['name','key']
{'name':
>>>
['name',
2
>>> dict1.values()
['hellow',110]
['hellow',
2
3
4
>>> dict2 = {'name1': 'world', 'key': 100}
>>>dict1.update(dict2)
>>>dict1
{'name':'hellow', 'key': 100, 'name1': 'world'}
>>>
>>>
{'name':
2
3
4
5
>>> dict1 = ddict.copy()
>>>dict1
{'y':None, 'x': None}
>>>ddict
{'y':None, 'x': None}
>>>
{'y':
>>>
{'y':
三、集合类型
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> s = set('check')
>>>s
set(['h','c', 'e', 'k'])
>>>'k' in s
True
>>>'c' not in s
False
>>>s.add('z')
>>>s
set(['h','c', 'z', 'e', 'k'])
>>>s.update('python')
>>>s
set(['c','e', 'h', 'k', 'o', 'n', 'p', 't', 'y', 'z'])
>>>s.remove('z')
>>>s
set(['c','e', 'h', 'k', 'o', 'n', 'p', 't', 'y'])
>>>s -= set('python')
>>>s
set(['c','e', 'k'])
>>>del s
>>>s
Traceback(most recent call last):
File "", line 1, in
NameError:name 's' is not defined
>>>
set(['h',
>>>
True
>>>
False
>>>
>>>
set(['h',
>>>
>>>
set(['c',
>>>
>>>
set(['c',
>>>
>>>
set(['c',
>>>
>>>
Traceback
NameError:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> s = set('check')
>>>t = set('python')
>>>s
set(['h','c', 'e', 'k'])
>>>t
set(['h','o', 'n', 'p', 't', 'y'])
>>>s|t
set(['c','e', 'h', 'k', 'o', 'n', 'p', 't', 'y'])
>>>
>>>s&t
set(['h'])
>>>
>>>s - t
set(['c','e', 'k'])
>>>
>>>s ^ t
set(['p','c', 'e', 't', 'y', 'k', 'o', 'n'])
>>>
>>>
set(['h',
>>>
set(['h',
>>>
set(['c',
>>>
>>>
set(['h'])
>>>
>>>
set(['c',
>>>
>>>
set(['p',
0 0
- 映像和集合类型
- 映像和集合类型
- python映像和集合类型
- Python:映像和集合类型
- Python--映像和集合类型(字典)
- Python核心编程----映像和集合类型
- python的序列,映像和集合类型
- Python核心编程(第七章)--映像和集合类型
- 《Python核心编程》第7章 映像和集合类型 练习
- python核心编程(第二版)参考答案(自制)--第七章·映像和集合类型
- python核心编程五——映像和集合
- MIC2映像类型
- 弱类型集合和强类型集合
- 集合接口和类型
- 异常和集合类型
- 加载内核映像和根文件系统映像
- Hashtable 和 Dictionary 集合类型
- Hibernate_映射简单集合类型和组件集合类型
- servlet开发时HTTP Status 405
- 序列类型操作符(关系、连接、重复…
- C语言中运算符的优先级
- Sublime不可不知的实用技巧
- c语言实现字符串的逆序输出
- 映像和集合类型
- range语法与列表解析
- SpringMVC工作原理
- 自定义Git配置
- 深入理解wait(),notify(),notifyAll()
- 一个注册、登陆系统的实现
- redis 中 SETBIT命令的用法
- 命令行出现ping、ipconfig等命令不…
- NoSQL简介