python- 字典

本贴最后更新于 421 天前,其中的信息可能已经天翻地覆

一、字典的特性

1、通过花括号定义,以键值对的形式存在(key:value)

2、同一个字典中key不能重复,value可以重复

3、字典是无序的

test_dict = {"key":"value","key1":"value1"}

二、字典的创建

1、直接花括号赋值:

test_dict = {"key":"value","key1":"value1"}

2、通过dict()创建

test_dict1 = dict(a=1,b=2,c=3)

print(test_dict1)

结果:{'a': 1, 'b': 2, 'c': 3}

3、通过dict.fromkeys(list1,list2)

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

list2 = ["abc"]

test_dict3 = dict.fromkeys(list1,list2)

print(test_dict3)

结果:{'a': ['abc'], 'b': ['abc'], 'c': ['abc']}

4、通过dict(zip(list1,list2))创建

list1 = ["a","b","c"]
list2 = ["1","2","3"]
test_dict = dict(zip(list1,list2))
print(test_dict)
结果:{'a': '1', 'b': '2', 'c': '3'}

三、字典取值

test_dict = {"a":"1","b":"2","c":3}

1、通过key取值

result = test_dict["a"]

print(result)

结果:1

2、通过字典的.get("key"),不存在的时候默认返回None,不会报错

test_dict = {"a":"1","b":"2","c":3}
result1 = test_dict.get("b")
result = test_dict.get("bb")
print(result1)
结果:2
print(result)
结果:None

3、.keys()获取所有的key

result = test_dict.keys()

print(result,type(result))#取出的数据类型是dict_keys,不能直接取值,需要转换成list来用

结果:dict_keys(['a', 'b', 'c']) <class 'dict_keys'>

4、.values()获取所有的value

result = test_dict.values()

print(result,type(result))#取出的数据类型是dict_values,不能直接取值,需要转换成list来用

结果:dict_values(['1', '2', 3]) <class 'dict_values'>

5、.items()获取所有的键值对

result = test_dict.items()

print(result,type(result))#取出的数据类型是dict_items,不能直接取值,需要转换成list来用

结果:dict_items([('a', '1'), ('b', '2'), ('c', 3)]) <class 'dict_items'>

result1 = list(result)

result2 = dict(result1)

print(result2)

结果:{'a': '1', 'b': '2', 'c': 3}

6、遍历

(1)遍历所有的key

for key in test_dict.keys():

print(key)

结果:

a
b
c

(2)遍历所有的value

for value in test_dict.values():
 print(value)
结果:
1
2
3

(3)遍历所有键值对
for key,value in test_dict.items():
print(key,value)
结果:
a 1
b 2
c 3

四、字典修改

1、通过key修改对应value

(1)只可以改value,不可以改key

test_dict = {"a":"1","b":"2","c":3}

test_dict["a"] = 4

print(test_dict)

结果:{'a': 4, 'b': '2', 'c': 3}

(2)如果key不存在,就相当于新增

test_dict["aa"] = "test"

print(test_dict)

结果:{'a': 4, 'b': '2', 'c': 3, 'aa': 'test'}

2、新增

(1)直接新增

test_dict["bb"] = "python"

print(test_dict)

结果:{'a': 4, 'b': '2', 'c': 3, 'aa': 'test','bb':'python'}

(2).setdefault(key,value),key不存在新加一对,key存在则不变

--如果字典中有则不变

test_dict.setdefault("a","value")

print(test_dict)

结果:{'a': 4, 'b': '2', 'c': 3, 'aa': 'test', 'bb': 'python'}

--如果字典中没有则添加一个键值对

test_dict.setdefault("cc","value")

print(test_dict)

结果:{'a': 4, 'b': '2', 'c': 3, 'aa': 'test', 'bb': 'python', 'cc': 'value'}

3、合并字典

test_dict1 = {"a":"1","b":"2","c":3}

test_dict2 = {"d":"4","e":"5","f":6}

将test_dict2合并到test_dict1(修改test_dict1)(test_dict2不会动),如果test_dict2与test_dict1有相同的key,则会把test_dict1的值,更新为test_dict2的值

test_dict1.update(test_dict2)

print(test_dict1)

结果:

{'a': '1', 'b': '2', 'c': 3, 'd': '4', 'e': '5', 'f': 6}

五、字典删除

1、通过del删除,无返回值

test_dict = {"a":"1","b":"2","c":3}

del test_dict["a"]

print(test_dict)

结果:{'b': '2', 'c': 3}

2、通过.pop("key")删除,返回删除的value

test_dict = {"a":"1","b":"2","c":3}

result = test_dict.pop("a")

print(result)

结果:1

3、.popitem()随机删除一个,以元组形式返回被删除的键值对(key,value)

以python进栈出栈顺序删除:后进先出原则

image.png

E先出,依次往后删除

test_dict = {"a":"1","b":"2","c":3}

result = test_dict.popitem()

print(result)

结果:('c', 3)

4、.clear()清空字典

test_dict = {"a":"1","b":"2","c":3}

test_dict.clear()

print(test_dict)

结果:{}

六、字典排序

1、根据ASCII码来排序的

2、sorted(key[或value],reverse)

test_dict = {"a":"5","b":"3","c":1}

result = sorted(test_dict.keys(),reverse=True)
print(result)

结果:['c', 'b', 'a']

3、sorted(test_dict.items(),key=operator.itemgetter(0))

itemgetter(0)中的0代表根据key排序,1代表根据value排序

test_dict = {"c":"5","b":"3","a":"6"}

result = sorted(test_dict.items(),key=operator.itemgetter(0))

排序后返回的类型是list类型,需要手动转换为dict类型

print(dict(result))

结果:{'a': '6', 'b': '3', 'c': '5'}

1 操作
huoshan 在 2023-03-02 16:42:09 更新了该帖
回帖
请输入回帖内容 ...