元组、字典的常用方法

简介:

一、元组的常用方法


①元组方法-------count(),元组中某个元素出现的次数

1
2
3
4
5
6
7
8
9
10
>>> t_tuple = ( 'a' , 'b' , 11 , 22 )
>>> t_tuple.count( 'a' )
1
>>> t_tuple.count( 'b' )
1
>>> t_tuple.count( '11' )
0
>>> t_tuple.count( 11 )
1
>>>

①元组方法-------index(),查找出元组中元素的下标位置,如果元素不存在,则会报错

1
>>> t_tuple.index( 'a' ) 0 >>> t_tuple.index( 'b' ) 1 >>> t_tuple.index( 11 ) 2 >>> t_tuple.index( 'c' )Traceback (most recent call last):   File  "<stdin>" , line  1 in  <module>ValueError:  tuple .index(x): x  not  in  tuple >>>

二、字典及常用方法


①字典常用方法-------clear(),清除字典内容


1
2
3
4
5
6
7
>>> name_dict = { 'alex' : 22 , 'eric' : 26 , 'tony' : 25 }
>>> name_dict
{ 'tony' 25 'alex' 22 'eric' 26 }
>>> name_dict.clear()
>>> name_dict
{}
>>>



浅拷贝

字典常用方法-------copy(),字典浅拷贝,该拷贝纸拷贝第一层,如果子点的key或者value下还继续有字典,

1
>>> name_dict = { 'alex' : 22 , 'eric' : 26 , 'tony' : 25 }>>> name_dict.copy(){ 'tony' 25 'alex' 22 'eric' 26 }>>> name_dicts  =  name_dict.copy()>>>  print  name_dicts{ 'tony' 25 'alex' 22 'eric' 26 }>>>  id (name_dict) 39525232 >>>  id (name_dicts) 39524800 >>>

深拷贝

import copy

test_dict = {'a':{'b':{'c':100}}}

test02_dict=copy.deepcopy(被拷贝的字典),拷贝多层,当被拷贝的字典中二层或者二层以上中的键对应的值发生变化,通过该方法拷贝得到的字典test002_dict中的键值是不会发生变化的


为什么要拷贝?
1当进行修改时,想要保留原来的数据和修改后的数据

数字字符串 和 集合 在修改时的差异? (深浅拷贝不同的终极原因)
1在修改数据时:
2数字字符串:在内存中新建一份数据
3集合:修改内存中的同一份数据

对于集合,如何保留其修改前和修改后的数据?
1在内存中拷贝一份



字典常用方法-------get(),获取某个key对应的值,如果该key不存在,返回None,也可以指定返回其他结果,get方法可以避免在key不存在的时候返回报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> name_dict
{ 'tony' 25 'alex' 22 'eric' 26 }
>>> name_dict.get( 'alex' )
22
>>> name_dict[ 'alex' ]
22
>>> name_dict.get( 'eric' )
26
>>> name_dict.get( 'susan' )
>>>  print  name_dict.get( 'susan' )
None
>>>  print  name_dict.get( 'susan' , 'OK' )
OK
>>> name_dict.get( 'susan' , 'OK' )
'OK'
>>>  print  name_dict[ 'susan' ]
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
KeyError:  'susan'
>>>

④字典一般用字符串,数字,类的实例来作为key


⑤判断一个字典是否为字典用如下方法:

1
2
3
4
>>> nu_dict  =  { 'alex' : 18 , 'tony' : 22 }
>>>  type (nu_dict)< type  'dict' >
>>>  type (nu_dict)  is  dictTrue
>>>

字典常用方法-------fromkeys(),可以将一个列表与定义的后面的一个值进行匹配,生成一个新的字典,该方法可以用于一个人名列表,如果要把人名列表对应每个人的个人信息,可以用到次方法

1
2
3
4
>>>a{ 'a' 1 'c' 3 'b' 2 }
>>>a.fromkeys([ 1 , 2 , 3 , 4 , 5 ], 't' ){ 1 't' 2 't' 3 't' 4 't' 5 't' }
>>>
>>>a.fromkeys([ 'alex' , 'eric' , 'tony' , 'susan' ],[]){ 'tony' : [],  'alex' : [],  'eric' : [],  'susan' : []}

字典常用方法-------items(),取出字典中所有对应的key:value

1
2
3
4
5
6
>>>name_dict  =  { 'alex' : 22 , 'eric' : 24 , 'tony' : 18 }
>>>name_dict
{ 'tony' 18 'alex' 22 'eric' 24 }
>>>name_dict.items()
[( 'tony' 18 ), ( 'alex' 22 ), ( 'eric' 24 )]
>>>


对于字典中的内容不是很多,即上百条或者上千条更甚至上万条可以用以上方法,如果上百万或千万条记录,则建议用如下方法:

1
2
3
4
5
6
>>>  for  in  name_dict: print  k,name_dict[k]
...
tony  18
alex  22
eric  24
>>>

这样好处是只读取了字典中的key,生成一个列表,然后再拿着key去字典中找对应的value,可以节约内存,而上面的方法是在内存中生成两个列表,这样就消耗了过多的内存资源。

字典常用方法-------keys(),当前所有的key打印出来

1
>>> name_dict{ 'tony' 18 'alex' 22 'eric' 24 }>>> name_dict.keys()[ 'tony' 'alex' 'eric' ]>>>

字典常用方法-------pop(),删除指定的键值对,只需要指定键即可

1
2
3
4
5
6
7
>>> name_dict
{ 'tony' 18 'alex' 22 'eric' 24 }
>>> name_dict.pop( 'alex' )
22
>>> name_dict
{ 'tony' 18 'eric' 24 }
>>>


字典常用方法-------del,该方法是一个全局性的,可以删除一个字典,列表,变量,元组等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> a = 'alex'
>>>  del  a
>>> a
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
NameError: name  'a'  is  not  defined
>>> b  =  { 'ccc' : 111 , 'ddd' : 222 }
>>>  del  b
>>> b
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
NameError: name  'b'  is  not  defined
>>> c  =  ( 'a' , 2 , 'c' , 'f' , 22 ,)
>>> c
( 'a' 2 'c' 'f' 22 )
>>>  del  c
>>> c
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
NameError: name  'c'  is  not  defined
>>>

字典常用方法-------setdefault()如果字典的键对应的值存在,则输出,不存在则定义一个默认值

1
>>> c{ 7 : [],  8 : [],  9 : [],  10 : [],  11 : [],  12 : [],  13 : [],  14 : [],  15 : [],  16 : [],  17 : [],  18 : [],  19 : [],  20 : [],  21 : [],  22 : [],  23 : [],  24 : [],  25 : [],  26 : [],  27 : [],  28 : [],  29 : [],  30 : [],  31 : [],  32 : [],  33 : [],  34 : [],  35 : [],  36 : [],  37 : [],  38 : [],  39 : [],  40 : [],  41 : [],  42 : [],  43 : [],  44 : [],  45 : [],  46 : [],  47 : [],  48 : [],  49 : [],  50 : [],  51 : [],  52 : [],  53 : [],  54 : [],  55 : [],  56 : [],  57 : [],  58 : [],  59 : [],  60 : [],  61 : [],  62 : [],  63 : [],  64 : [],  65 : [],  66 : [],  67 : [],  68 : [],  69 : [],  70 : [],  71 : [],  72 : [],  73 : [],  74 : [],  75 : [],  76 : [],  77 : [],  78 : [],  79 : [],  80 : [],  81 : [],  82 : [],  83 : [],  84 : [],  85 : [],  86 : [],  87 : [],  88 : [],  89 : [],  90 : [],  91 : [],  92 : [],  93 : [],  94 : [],  95 : [],  96 : [],  97 : [],  98 : [],  99 : []}>>> c.setdefault( 8 )[]>>> c.setdefault( 200 )>>> c{ 7 : [],  8 : [],  9 : [],  10 : [],  11 : [],  12 : [],  13 : [],  14 : [],  15 : [],  16 : [],  17 : [],  18 : [],  19 : [],  20 : [],  21 : [],  22 : [],  23 : [],  24 : [],  25 : [],  26 : [],  27 : [],  28 : [],  29 : [],  30 : [],  31 : [],  32 : [],  33 : [],  34 : [],  35 : [],  36 : [],  37 : [],  38 : [],  39 : [],  40 : [],  41 : [],  42 : [],  43 : [],  44 : [],  45 : [],  46 : [],  47 : [],  48 : [],  49 : [],  50 : [],  51 : [],  52 : [],  53 : [],  54 : [],  55 : [],  56 : [],  57 : [],  58 : [],  59 : [],  60 : [],  61 : [],  62 : [],  63 : [],  64 : [],  65 : [],  66 : [],  67 : [],  68 : [],  69 : [],  70 : [],  71 : [],  72 : [],  73 : [],  74 : [],  75 : [],  76 : [],  77 : [],  78 : [],  79 : [],  80 : [],  81 : [],  82 : [],  83 : [],  84 : [],  85 : [],  86 : [],  87 : [],  88 : [],  89 : [],  90 : [],  91 : [],  92 : [],  93 : [],  94 : [],  95 : [],  96 : [],  97 : [],  98 : [],  99 : [],  200 None }>>> c.setdefault( 200 , 'cccc' )>>> c{ 7 : [],  8 : [],  9 : [],  10 : [],  11 : [],  12 : [],  13 : [],  14 : [],  15 : [],  16 : [],  17 : [],  18 : [],  19 : [],  20 : [],  21 : [],  22 : [],  23 : [],  24 : [],  25 : [],  26 : [],  27 : [],  28 : [],  29 : [],  30 : [],  31 : [],  32 : [],  33 : [],  34 : [],  35 : [],  36 : [],  37 : [],  38 : [],  39 : [],  40 : [],  41 : [],  42 : [],  43 : [],  44 : [],  45 : [],  46 : [],  47 : [],  48 : [],  49 : [],  50 : [],  51 : [],  52 : [],  53 : [],  54 : [],  55 : [],  56 : [],  57 : [],  58 : [],  59 : [],  60 : [],  61 : [],  62 : [],  63 : [],  64 : [],  65 : [],  66 : [],  67 : [],  68 : [],  69 : [],  70 : [],  71 : [],  72 : [],  73 : [],  74 : [],  75 : [],  76 : [],  77 : [],  78 : [],  79 : [],  80 : [],  81 : [],  82 : [],  83 : [],  84 : [],  85 : [],  86 : [],  87 : [],  88 : [],  89 : [],  90 : [],  91 : [],  92 : [],  93 : [],  94 : [],  95 : [],  96 : [],  97 : [],  98 : [],  99 : [],  200 None }>>>

字典常用方法-------update(),将两个字典进行整合

1
2
3
4
5
6
>>> ss  =  { 'a' : 11 , 'bb' : 22 , 'cc' : 33 }
>>> tt  =  { 'a' : 'kk' , 'pp' : 23 , 'rt' : 33 }
>>> ss.update(tt)
>>> ss
{ 'a' 'kk' 'rt' 33 'pp' 23 'bb' 22 'cc' 33 }
>>>

如果ss中的键和tt中的键有重复,则以tt中的键所对应的值替换掉ss中keys对应的值,如果tt中的键值对在ss中不存在,则在ss中进行创建


字典常用方法-------values(),打印字典中所有的值

1
2
3
4
5
>>> ss
{ 'a' 'kk' 'rt' 33 'pp' 23 'bb' 22 'cc' 33 }
>>> ss.values()
[ 'kk' 33 23 22 33 ]
>>>






      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1715788 ,如需转载请自行联系原作者


相关文章
|
7月前
|
存储 程序员 Python
Python列表元组字典集合存储结构1
Python列表元组字典集合存储结构
40 0
|
27天前
|
索引 Python 存储
Python 04 之变量【列表,元组,集合,字典,字符串】
Python 04 之变量【列表,元组,集合,字典,字符串】
53 0
Python 04 之变量【列表,元组,集合,字典,字符串】
|
6月前
|
Python
python之集合的创建与使用,遍历,集合常见的操作函数,集合与列表,元组,字典的嵌套
python之集合的创建与使用,遍历,集合常见的操作函数,集合与列表,元组,字典的嵌套
|
7月前
|
存储 Python
Python列表元组字典集合存储结构 2
Python列表元组字典集合存储结构
38 0
|
7月前
|
索引 Python
python 中的列表,元组,字典,集合
python 中的列表,元组,字典,集合
76 1
|
8月前
元组和列表转换成字典
元组和列表转换成字典
40 0
|
10月前
|
算法 Python 容器
字典的相关应用
字典的相关应用
46 0
|
11月前
|
存储 索引 Python
列表、字典、集合、元组
列表、字典、集合、元组
45 0
对象转字典、字典排序
对象转字典、字典排序
79 0
使用内置字典
使用内置字典
50 0

热门文章

最新文章