Python2 字典總結

2021-09-03 01:33:01 字數 4577 閱讀 4593

a = 

print str(a).decode('string_escape')

# 輸出:

a = ["a", "b", "c"]

b = dict.fromkeys(a)

print b

# 輸出:

可以自定義預設值

a = ["a", "b", "c"]

b = dict.fromkeys(a, 250)

print b

# 輸出:

import collections

data = collections.ordereddict()

data["a"] = "1"

data["b"] = "2"

data["c"] = "3"

print data

# 輸出:ordereddict([('a', '1'), ('b', '2'), ('c', '3')])

import json

from collections import ordereddict

jsonstr = ''

configdict = json.loads(jsonstr, object_pairs_hook=ordereddict)

print configdict

# 輸出:ordereddict([(u'a', 1), (u'b', 2), (u'c', 3)])

a = 

print(a['a'])

# 輸出:123

a = 

print(a.get('e'))

# 輸出:none

a = 

print(a.get('key', '404'))

# 輸出:404

a = 

print a.keys()

# 輸出:['a', 'c', 'b', 'd']

a = 

print str(a.values()).decode('string_escape')

# 輸出: [123, 'pen', 456, '嘿嘿']

a = 

if 'a' in a:

print('yes')

# 輸出:yes

a = 

for k, v in a.items():

print str((k, v)).decode('string_escape')

# 輸出:

# ('a', 123)

# ('c', 'pen')

# ('b', 456)

# ('d', '嘿嘿')

a = 

for kv in a.items():

print str(kv).decode('string_escape')

# 輸出:

# ('a', 123)

# ('c', 'pen')

# ('b', 456)

# ('d', '嘿嘿')

a = 

for k in a:

print str((k, a[k])).decode('string_escape')

print str((k, a.get(k))).decode('string_escape')

# 輸出:

# ('a', 123)

# ('a', 123)

# ('c', 'pen')

# ('c', 'pen')

# ('b', 456)

# ('b', 456)

# ('d', '嘿嘿')

# ('d', '嘿嘿')

a = 

a['e'] = 250

print str(a).decode('string_escape')

# 輸出:

a['a'] = "hehe"

print str(a).decode('string_escape')

# 輸出:

當用來修改時不起作用

a = 

a.setdefault('a', 250)

print str(a).decode('string_escape')

# 輸出:

新增字典

a = 

a.setdefault('e', 250)

print str(a).decode('string_escape')

# 輸出:

不設預設值時,預設為none

a = 

a.setdefault('f')

print str(a).decode('string_escape')

# 輸出:

a = 

b =

a.update(b)

print str(a).decode('string_escape')

# 輸出:

a = 

del a["d"]

print a

# 輸出:

刪除字典給定鍵 key 所對應的值,返回值為被刪除的值,key值必須給出,否則,返回default值

a = 

b = a.pop("a")

print a

# 輸出:

print b

# 輸出:123

a = 

b = a.popitem()

print a

# 輸出:

print b

# 輸出:('a', 123)

a = 

b = a.clear()

print a

# 輸出:{}

用於判斷鍵是否存在於字典中,如果鍵在字典dict裡返回true,否則返回false

a = 

print a.has_key("a")

# 輸出:true

a = 

print a.items()

# 輸出:[('a', 123), ('c', 'pen'), ('b', 456)]

print(list(a.items()))

# 輸出:[('a', 123), ('c', 'pen'), ('b', 456)]

a = 

print len(a)

# 輸出:3

a = 

print str(a)

print type(str(a))

# 輸出:

# 輸出:

5.1.字典轉字串

import json

a =

print json.dumps(a)

# 輸出:

print type(json.dumps(a))

# 輸出:

5.2 字串轉字典

import json

a = ''

print json.loads(a)

# 輸出:

print type(json.loads(a))

# 輸出:

淺拷貝: 引用物件

淺拷貝:深拷貝父物件(一級目錄),子物件(二級目錄)不拷貝,還是引用

a = 

b = a

c = a.copy()

# 修改 data 資料

a['user'] = 'root'

a['num'].remove(1)

print(a)

# # 輸出:

print(b)

# # 輸出:

print(c)

# # 輸出:

import copy

a =

b = copy.deepcopy(a)

# 修改 data 資料

a['user'] = 'root'

a['num'].remove(1)

print a

# # 輸出:

print b

# 輸出:

Python2 字典 has key 方法

python2 字典 has key 方法用於判斷鍵 key 是否存在於字典 d 中,如果鍵在字典中返回true,否則返回false。官方文件推薦用 in 操作符,因為它更短更通俗易懂。has key 是老舊遺留的api,為了支援2.2之前的 留下的。python3已經刪除了該函式。has key ...

Python2和Python3字典 字典序的區別

在python2中,python2 d dict b 12 a 12 print d.keys a b 在python3中,python3 d dict b 12 a 12 print d.keys dict keys b a 可見python2,python3的字典keys的結果是不一樣的。因此,...

python基礎2 字典

alien 0 訪問字典中的值 alien 0 color 新增鍵值對 alien 0 x position 0,鍵值對的排列順序和新增順序沒有關係 建立乙個空字典 alien 0 修改字典中的值 alien 0 x position 2刪除鍵值對 del alien 0 color 遍歷鍵值 fo...