导航:首页 > 编程语言 > python怎么清理字典

python怎么清理字典

发布时间:2023-01-06 14:30:10

python字典操作函数

字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一内建的映射类型,基本的操作包括如下:

(1)len():返回字典中键—值对的数量;

(2)d[k]:返回关键字对于的值;

(3)d[k]=v:将值关联到键值k上;

(4)del d[k]:删除键值为k的项;

(5)key in d:键值key是否在d中,是返回True,否则返回False。

(6)clear函数:清除字典中的所有项

(7)函数:返回一个具有相同键值的新字典;deep()函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题

(8)fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

(9)get函数:访问字典成员

(10)has_key函数:检查字典中是否含有给出的键

(11)items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

(12)keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

(13)pop函数:删除字典中对应的键

(14)popitem函数:移出字典中的项

(15)setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

(16)update函数:用一个字典更新另外一个字典

(17) values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

一、字典的创建

1.1 直接创建字典

d={'one':1,'two':2,'three':3}

printd

printd['two']

printd['three']

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

>>>

1.2 通过dict创建字典

# _*_ coding:utf-8 _*_

items=[('one',1),('two',2),('three',3),('four',4)]

printu'items中的内容:'

printitems

printu'利用dict创建字典,输出字典内容:'

d=dict(items)

printd

printu'查询字典中的内容:'

printd['one']

printd['three']

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

items中的内容:

[('one',1), ('two',2), ('three',3), ('four',4)]

利用dict创建字典,输出字典内容:

{'four':4,'three':3,'two':2,'one':1}

查询字典中的内容:

>>>

或者通过关键字创建字典

# _*_ coding:utf-8 _*_

d=dict(one=1,two=2,three=3)

printu'输出字典内容:'

printd

printu'查询字典中的内容:'

printd['one']

printd['three']

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

输出字典内容:

{'three':3,'two':2,'one':1}

查询字典中的内容:

>>>

二、字典的格式化字符串

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

print"three is %(three)s."%d

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

threeis3.

>>>

三、字典方法

3.1 clear函数:清除字典中的所有项

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3,'four':4}

printd

d.clear()

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'four':4,'three':3,'two':2,'one':1}

{}

>>>

请看下面两个例子

3.1.1

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d={}

printd

printdd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{'two':2,'one':1}

>>>

3.1.2

# _*_ coding:utf-8 _*_

d={}

dd=d

d['one']=1

d['two']=2

printdd

d.clear()

printd

printdd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'two':2,'one':1}

{}

{}

>>>

3.1.2与3.1.1唯一不同的是在对字典d的清空处理上,3.1.1将d关联到一个新的空字典上,这种方式对字典dd是没有影响的,所以在字典d被置空后,字典dd里面的值仍旧没有变化。但是在3.1.2中clear方法清空字典d中的内容,clear是一个原地操作的方法,使得d中的内容全部被置空,这样dd所指向的空间也被置空。

3.2 函数:返回一个具有相同键值的新字典

# _*_ coding:utf-8 _*_

x={'one':1,'two':2,'three':3,'test':['a','b','c']}

printu'初始X字典:'

printx

printu'X复制到Y:'

y=x.()

printu'Y字典:'

printy

y['three']=33

printu'修改Y中的值,观察输出:'

printy

printx

printu'删除Y中的值,观察输出'

y['test'].remove('c')

printy

printx

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

初始X字典:

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

X复制到Y:

Y字典:

{'test': ['a','b','c'],'one':1,'three':3,'two':2}

修改Y中的值,观察输出:

{'test': ['a','b','c'],'one':1,'three':33,'two':2}

{'test': ['a','b','c'],'three':3,'two':2,'one':1}

删除Y中的值,观察输出

{'test': ['a','b'],'one':1,'three':33,'two':2}

{'test': ['a','b'],'three':3,'two':2,'one':1}

>>>

注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。deep函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题。

# _*_ coding:utf-8 _*_

fromimportdeep

x={}

x['test']=['a','b','c','d']

y=x.()

z=deep(x)

printu'输出:'

printy

printz

printu'修改后输出:'

x['test'].append('e')

printy

printz

运算输出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

输出:

{'test': ['a','b','c','d']}

{'test': ['a','b','c','d']}

修改后输出:

{'test': ['a','b','c','d','e']}

{'test': ['a','b','c','d']}

>>>

3.3 fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'])

printd

运算输出:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':None,'two':None,'one':None}

>>>

或者指定默认的对应值

# _*_ coding:utf-8 _*_

d=dict.fromkeys(['one','two','three'],'unknow')

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':'unknow','two':'unknow','one':'unknow'}

>>>

3.4 get函数:访问字典成员

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.get('one')

printd.get('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

1

None

>>>

注:get函数可以访问字典中不存在的键,当该键不存在是返回None

3.5 has_key函数:检查字典中是否含有给出的键

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.has_key('one')

printd.has_key('four')

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

True

False

>>>

3.6 items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

list=d.items()

forkey,valueinlist:

  printkey,':',value

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

three :3

two :2

one :1

>>>

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

it=d.iteritems()

fork,vinit:

  print"d[%s]="%k,v

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

d[three]=3

d[two]=2

d[one]=1

>>>

3.7 keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printu'keys方法:'

list=d.keys()

printlist

printu'\niterkeys方法:'

it=d.iterkeys()

forxinit:

  printx

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

keys方法:

['three','two','one']

iterkeys方法:

three

two

one

>>>

3.8 pop函数:删除字典中对应的键

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.pop('one')

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'three':3,'two':2}

>>>

3.9 popitem函数:移出字典中的项

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

d.popitem()

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':1}

{'two':2,'one':1}

>>>

3.10 setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值

# _*_ coding:utf-8 _*_

d={'one':1,'two':2,'three':3}

printd

printd.setdefault('one',1)

printd.setdefault('four',4)

printd

运算结果:

{'three':3,'two':2,'one':1}

{'four':4,'three':3,'two':2,'one':1}

>>>

3.11 update函数:用一个字典更新另外一个字典

# _*_ coding:utf-8 _*_

d={

  'one':123,

  'two':2,

  'three':3

  }

printd

x={'one':1}

d.update(x)

printd

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

{'three':3,'two':2,'one':123}

{'three':3,'two':2,'one':1}

>>>

3.12 values和itervalues函数:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重复的元素

# _*_ coding:utf-8 _*_

d={

  'one':123,

  'two':2,

  'three':3,

  'test':2

  }

printd.values()

运算结果:

=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======

[2,3,2,123]

>>>

Ⅱ python3怎么删除列表里指定的字典呢

i=list(filter(lambdax:x['name']!='wang',i))

Ⅲ Python 中如何删除字典元素

del dict[key]
可以查看帮助文档
help(dict)

Ⅳ python字典的基本操作

python字典的基本操作如下:

查询字典

字典里面可以嵌套字典,嵌套列表。

Ⅳ python中字典用完了怎么删除

您python中字典用完了为什么要删除呢?是想为了节省空间?
那么,若dict_1
=
{1:"abc",
2:"bcd",
c:"cde"}这样一个字典,你删除它就可以对它进行None的赋值,或者一个空字典的赋值。如下:
dict_1
=
None
输出
dict_1
就为None,也就是不输出
dict_1
=
{}
输出
{}
也就是一个空字典
这两个都起到了删除字典dict_1的效果
还有一种就是直接删除变量dict_1:del
dict_1
这个会导致整个变量的消失
print
dict_1
此时会出错,找不到该变量
若不懂,请追问,望采纳!

Ⅵ python中字典不用了怎么删除

如果你要删除整个变量,python里面垃圾是自动收集的,不用你太去关心。一定要用,就del 变量名。
如果你是要修改一个字典的内容,比如从里面删除一个元素,一般更推荐直接返回一个没有这个元素的新字典而不是去修改已有字典,del的效率太低了。一定要用就del a['b']这样就从字典a里面删除了key为b的那个值。

Ⅶ python dict用法

dic= {key1 : value1, key2 : value2 }

字典也被称作关联数组或哈希表。下面是几种常见的字典属性:

1、dict.clear()

clear() 用于清空字典中所有元素(键-值对),对一个字典执行 clear() 方法之后,该字典就会变成一个空字典。

2、dict.()

() 用于返回一个字典的浅拷贝。

3、dict.fromkeys()

fromkeys() 使用给定的多个键创建一个新字典,值默认都是 None,也可以传入一个参数作为默认的值。

4、dict.get()

get() 用于返回指定键的值,也就是根据键来获取值,在键不存在的情况下,返回 None,也可以指定返回值。

5、dict.items()

items() 获取字典中的所有键-值对,一般情况下可以将结果转化为列表再进行后续处理。

6、dict.keys()

keys() 返回一个字典所有的键。

Ⅷ python中怎么去除字典

不是很明白你表达的意思,你是想删除整个字典 ,还是其中一个键,或者调出字典里的参数

Ⅸ Python 中如何删除字典元素

要删除一个key,用pop(key)方法,对应的value也会从dict中删除
d
=
{'michael':
95,
'bob':
75,
'tracy':
85}
>>>
d.pop('bob')
75
>>>
d
{'michael':
95,
'tracy':
85}

Ⅹ python 字典如何删除

代码写得不错。远程登陆后检查内存和磁盘空间。轻量级的运维监控。

但是你的代码里没有字典。 字典的元素删除很简单。比如有一个字典元素d['host']

删除只需要del d['host']

python里的对象,基本上是指针方式的都可以用这个方法删除。不过数组与链接麻烦些。

阅读全文

与python怎么清理字典相关的资料

热点内容
dvd光盘存储汉子算法 浏览:755
苹果邮件无法连接服务器地址 浏览:958
phpffmpeg转码 浏览:669
长沙好玩的解压项目 浏览:140
专属学情分析报告是什么app 浏览:562
php工程部署 浏览:831
android全屏透明 浏览:730
阿里云服务器已开通怎么办 浏览:801
光遇为什么登录时服务器已满 浏览:300
PDF分析 浏览:483
h3c光纤全工半全工设置命令 浏览:141
公司法pdf下载 浏览:381
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:682
如何取消命令方块指令 浏览:347
风翼app为什么进不去了 浏览:777
im4java压缩图片 浏览:361
数据查询网站源码 浏览:148
伊克塞尔文档怎么进行加密 浏览:889
app转账是什么 浏览:162