導航:首頁 > 編程語言 > python查詢字典一個鍵的值

python查詢字典一個鍵的值

發布時間:2022-12-31 00:08:36

python中怎麼取出字典的鍵

舉例如下:

1、新增python文件,testdictkey.py;

㈡ python中兩個字典間根據相同的鍵篩選其對應的值

Python Code:

d1={'k1':'v1','k2':'v2','k3':'v3'}
d2={'k1':'v1_1','k3':'v3_3','k4':'v4'}
print(list(map(lambdak:(d1[k],d2[k]),{*d1}&{*d2})))

輸出:

[('v3','v3_3'),('v1','v1_1')]

上述演示代碼中 d1 和 d2 這兩個字典的相同鍵都是:k1 和 k3,

其對應值為:v1, v1_1 和 v3, v3_3,輸出時列表內部以元組的形式返回了這兩個相同建的值。

㈢ 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]

>>>

㈣ python 檢測字典是否含有某個鍵值

a = { }
a.has_key()就知道了
如 字典 a = {1:'aaa',2:'bbb','abc':'ccc','3':'ddd'}
查詢字典中是否有鍵 1
a.has_key(1),如果是返回True,否則返回False
查詢是否有鍵 'abc'
a.has_key('abc')
查詢是否有鍵 '3'
a.has_key('3')

查詢是否有值是'aaa'
'aaa' in a.values() 或'aaa' == a.values() 如果是返回True,否則返回False

㈤ python字典鍵中含有變數怎麼查詢值

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

price={"proct(1)":"100","proct(2)":"58","proct(3)":"20.8",}


defproct(num):
"getprice"
get_price="proct(%d)"%num
printprice[get_price]

button=raw_input("輸入一個數字:")
proct(button)
proct(1)#100

這是其中一個方式

㈥ python中怎麼改變一個字典的對應鍵的值

最為簡單的方法是利用表理解,生成一個新的字典
必須要保證鍵值是一一對應的
d = {'one':1, 'two':2, 'three':3, 'four':4}
di = {v:k for k,v in d.items()}
di[1]

㈦ python作業:讓用戶輸入鍵和值生成字典,然後通過值查找對應的鍵

#以輸入3個值和鍵為例
dic={}
foriinrange(0,3):
key=input("輸入建:")
val=input("輸入值:")
ifkeynotindic.keys():
dic[key]=val

print("輸入完成。")
key=input("輸入要查找的鍵:")
ifkeyindic.keys():
print(key,'的值為:',dic[key])
else:
print('字典中沒有這個鍵')

效果如下:

㈧ python怎樣輸出字典鍵對應的值

dic={"233":"233",233:"666"}
printdic["233"]
printdic[233]

也就是說,對於一個字典dic,dic[鍵值]就是該鍵值所對應的值

㈨ python怎麼取字典第一個value

1、首先打開python編輯器,寫上注釋內容,如下圖所示。

閱讀全文

與python查詢字典一個鍵的值相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:736
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163