A004. Python 数据类型 ' 组 ‘

 

【 Python中的'组' 】


=== 4-1 列表===

1. 列表 [] 并且列表可以嵌套
>>> type([])
<class 'list'>

2. 列表访问
>>> [1,2,3,'admin','pass'][2:-1]
[3, 'admin']

3. 列表的运算
>>> [1,2]+ ['admin','root']
[1, 2, 'admin', 'root']

>>> ['admin',0,'root',True] * 2
['admin', 0, 'root', True, 'admin', 0, 'root', True]


=== 4-2 元组===

1. 元组 () 并且元组可以嵌套
>>> type( () )
<class 'tuple'>

2. 元组访问
>>> (1,2,3,'admin','pass')[2:-1]
(3, 'admin')

3. 元组的运算
>>> (1,2)+ ('admin','root')
(1, 2, 'admin', 'root')

>>> ('admin',0,'root',True) * 2
('admin', 0, 'root', True, 'admin', 0, 'root', True)

4. 元组特殊
>>> type( (1) )
<class 'int'>
>>> type( ('root') )
<class 'str'>
>>> type( (False) )
<class 'bool'>
>>> type( ([1,2,3]) )
<class 'list'>

>>> type( (1,'admin') )
<class 'tuple'>

5. 定义单一元素的元组
>>> type( (1, ) )
<class 'tuple'>
>>> (1,)
(1,)
空元组
>>> type( () )
<class 'tuple'>


=== 4-3 序列和切片.. ===

1. 总结:
序列: 有序,序号。
切片: [6:3] [0:8:3]3位的切片

2. in  /  not in 
>>> 3 in [1,2,3,4]
True
>>> type(2 in (1,2,3))
<class 'bool'>
>>> 2 in (1,2,3)
True


>>> 2 not in (1,3,4,2)
False


3. 序列的计算  len() max() min() 

== len() ==
len([1,2,3])
>>> len( (2,3,'admin') )
3
>>> len('string')
6


== max() ==
>>> max('12403918')
'9'

== min() ==
>>> min( [1,2,3] )
1

== 字符串大小 asiic 转换函数 ord()==
>>> max('hello world js xy')
'y'
>>> min('admin')
'a'
>>> min('admin ')
' '

>>> type('2' in 'admin2')
<class 'bool'>
>>> '2' in 'admin2'
True

————————————————
>>> ord(' ')  计算asscii 码值 
32
>>> ord('d')
100
>>> ord('w')
119

 

 

=== 4-4 集合 set 无序.. ===
1 {}

>>> type({1,2,3})
<class 'set'>

>>> type({})
<class 'dict'>


2. 计算
>>> 1 in {1,2,4}
True
>>> 1 not in {1,2,3}
False

去交集
>>> {1,2,3,4,5} - {2,4,6}
{1, 3, 5}

取交集
>>> {1,2,3,4} & {1,5,4,3}
{1, 3, 4}

并集: 去重并集
>>> {1,2,3,4} | {2,3}
{1, 2, 3, 4}

3. set() 方法定义一个空的集合
>>> type(set({}))
<class 'set'>

 


=== 4-5 字典 dict .. ===

1. 字典 {key:value ... }

>>> type( {'key':'value', 'key2':'value2'} )
<class 'dict'>
>>> {1:1,2:2,23:'ersan'}
{1: 1, 2: 2, 23: 'ersan'}

2. dict 和 set区别 , set无key 只有value

3. 获取
>>> {'a':'run' , 'b':'go'}['b']
'go'

4. 注意 字典不能重复 键key

>>> {1:'runs','1':'run' , 'b':'go'}['1']
'run'
>>> {1:'runs','1':'run' , 'b':'go'}[1]
'runs'

5. 空的字典 { }

 

 

 

 

 

posted @ 2018-08-01 22:35  silvercell  阅读(1368)  评论(0)    收藏  举报