導航:首頁 > 編程語言 > 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怎麼清理字典相關的資料

熱點內容
PDF分析 瀏覽:482
h3c光纖全工半全工設置命令 瀏覽:135
公司法pdf下載 瀏覽:379
linuxmarkdown 瀏覽:347
華為手機怎麼多選文件夾 瀏覽:679
如何取消命令方塊指令 瀏覽:345
風翼app為什麼進不去了 瀏覽:774
im4java壓縮圖片 瀏覽:358
數據查詢網站源碼 瀏覽:146
伊克塞爾文檔怎麼進行加密 瀏覽:886
app轉賬是什麼 瀏覽:159
php的基本語法 瀏覽:792
對外漢語pdf 瀏覽:516
如何用mamp本地web伺服器 瀏覽:869
如何加密自己js代碼 瀏覽:627
排列組合a與c的演算法 瀏覽:534
如何在文件夾中找到同名內容 瀏覽:786
有什麼app文字轉韓文配音 瀏覽:372
循環宏1命令 瀏覽:35
斐波那契數列矩陣演算法 瀏覽:674