Python字典

词典 在Python中,是一个无序的数据值集合,用于像映射一样存储数据值,与其他数据类型不同,其他数据类型只将单个值作为一个元素保存,而Dictionary则保存 关键:价值 一对字典中提供了键值,使其更加优化。

null

注—— 字典中的键不允许多态性。

Disclamer: 需要注意的是,随着Python3.7的发布,字典已被修改以保持插入顺序,因此它们现在是数据值的有序集合。

创建字典

在Python中,可以通过在curly中放置一系列元素来创建字典 {} 大括号,用逗号分隔。Dictionary包含成对的值,一个是键,另一个对应的pair元素是它的 关键:价值 .字典中的值可以是任何数据类型,并且可以复制,而键不能重复,必须复制 不变的 .

注—— 字典密钥区分大小写,相同的名称但密钥的不同情况将被明确处理。

Python3

# Creating a Dictionary
# with Integer Keys
Dict = { 1 : 'Geeks' , 2 : 'For' , 3 : 'Geeks' }
print ( "Dictionary with the use of Integer Keys: " )
print ( Dict )
# Creating a Dictionary
# with Mixed keys
Dict = { 'Name' : 'Geeks' , 1 : [ 1 , 2 , 3 , 4 ]}
print ( "Dictionary with the use of Mixed Keys: " )
print ( Dict )


输出:

Dictionary with the use of Integer Keys: {1: 'Geeks', 2: 'For', 3: 'Geeks'}Dictionary with the use of Mixed Keys: {1: [1, 2, 3, 4], 'Name': 'Geeks'}

字典也可以通过内置函数dict()创建。只需将大括号{}放在上面,就可以创建一个空字典。

Python3

# Creating an empty Dictionary
Dict = {}
print ( "Empty Dictionary: " )
print ( Dict )
# Creating a Dictionary
# with dict() method
Dict = dict ({ 1 : 'Geeks' , 2 : 'For' , 3 : 'Geeks' })
print ( "Dictionary with the use of dict(): " )
print ( Dict )
# Creating a Dictionary
# with each item as a Pair
Dict = dict ([( 1 , 'Geeks' ), ( 2 , 'For' )])
print ( "Dictionary with each item as a pair: " )
print ( Dict )


输出:

Empty Dictionary: {}Dictionary with the use of dict(): {1: 'Geeks', 2: 'For', 3: 'Geeks'}Dictionary with each item as a pair: {1: 'Geeks', 2: 'For'}

嵌套字典:

图片[1]-Python字典-yiteyi-C++库

Python3

# Creating a Nested Dictionary
# as shown in the below image
Dict = { 1 : 'Geeks' , 2 : 'For' ,
3 :{ 'A' : 'Welcome' , 'B' : 'To' , 'C' : 'Geeks' }}
print ( Dict )


输出:

{1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}

向字典中添加元素

在Python Dictionary中,可以通过多种方式添加元素。通过定义值和键,可以一次向字典中添加一个值,例如Dict[key]=“value”。可以使用内置的 更新() 方法嵌套键值也可以添加到现有字典中。

注释- 添加值时,如果键值已经存在,则会更新该值,否则会向字典中添加一个具有该值的新键值。

Python3

# Creating an empty Dictionary
Dict = {}
print ( "Empty Dictionary: " )
print ( Dict )
# Adding elements one at a time
Dict [ 0 ] = 'Geeks'
Dict [ 2 ] = 'For'
Dict [ 3 ] = 1
print ( "Dictionary after adding 3 elements: " )
print ( Dict )
# Adding set of values
# to a single Key
Dict [ 'Value_set' ] = 2 , 3 , 4
print ( "Dictionary after adding 3 elements: " )
print ( Dict )
# Updating existing Key's Value
Dict [ 2 ] = 'Welcome'
print ( "Updated key value: " )
print ( Dict )
# Adding Nested Key value to Dictionary
Dict [ 5 ] = { 'Nested' :{ '1' : 'Life' , '2' : 'Geeks' }}
print ( "Adding a Nested Key: " )
print ( Dict )


输出:

Empty Dictionary: {}Dictionary after adding 3 elements: {0: 'Geeks', 2: 'For', 3: 1}Dictionary after adding 3 elements: {0: 'Geeks', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}Updated key value: {0: 'Geeks', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}Adding a Nested Key: {0: 'Geeks', 2: 'Welcome', 3: 1, 5: {'Nested': {'1': 'Life', '2': 'Geeks'}}, 'Value_set': (2, 3, 4)}

从字典中访问元素

要访问字典中的条目,请参考其键名。钥匙可以在方括号内使用。

Python3

# Python program to demonstrate
# accessing a element from a Dictionary
# Creating a Dictionary
Dict = { 1 : 'Geeks' , 'name' : 'For' , 3 : 'Geeks' }
# accessing a element using key
print ( "Accessing a element using key:" )
print ( Dict [ 'name' ])
# accessing a element using key
print ( "Accessing a element using key:" )
print ( Dict [ 1 ])


输出:

Accessing a element using key:ForAccessing a element using key:Geeks

还有一种方法叫做 得到() 这也有助于从字典中访问元素。

Python3

# Creating a Dictionary
Dict = { 1 : 'Geeks' , 'name' : 'For' , 3 : 'Geeks' }
# accessing a element using get()
# method
print ( "Accessing a element using get:" )
print ( Dict .get( 3 ))


输出:

Accessing a element using get:Geeks

访问嵌套字典的元素

要访问嵌套字典中任何键的值,请使用索引[]语法。

Python3

# Creating a Dictionary
Dict = { 'Dict1' : { 1 : 'Geeks' },
'Dict2' : { 'Name' : 'For' }}
# Accessing element using key
print ( Dict [ 'Dict1' ])
print ( Dict [ 'Dict1' ][ 1 ])
print ( Dict [ 'Dict2' ][ 'Name' ])


输出:

{1: 'Geeks'}GeeksFor

从字典中删除元素

使用del关键字

在Python字典中,可以使用 德尔 关键词。使用del关键字,可以删除字典以及整个字典中的特定值。嵌套字典中的项也可以通过使用del关键字并提供特定的嵌套键和要从该嵌套字典中删除的特定键来删除。

注: 这个 德尔迪克特 将删除整个词典,因此在删除后打印将引发错误。

Python3

# Initial Dictionary
Dict = { 5 : 'Welcome' , 6 : 'To' , 7 : 'Geeks' ,
'A' : { 1 : 'Geeks' , 2 : 'For' , 3 : 'Geeks' },
'B' : { 1 : 'Geeks' , 2 : 'Life' }}
print ( "Initial Dictionary: " )
print ( Dict )
# Deleting a Key value
del Dict [ 6 ]
print ( "Deleting a specific key: " )
print ( Dict )
# Deleting a Key from
# Nested Dictionary
del Dict [ 'A' ][ 2 ]
print ( "Deleting a key from Nested Dictionary: " )
print ( Dict )


输出:

Initial Dictionary: {'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 6: 'To', 7: 'Geeks'}Deleting a specific key: {'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 7: 'Geeks'}Deleting a key from Nested Dictionary: {'A': {1: 'Geeks', 3: 'Geeks'}, 'B': {1: 'Geeks', 2: 'Life'}, 5: 'Welcome', 7: 'Geeks'}

使用pop()方法

流行音乐( )方法用于返回和删除指定键的值。

Python3

# Creating a Dictionary
Dict = { 1 : 'Geeks' , 'name' : 'For' , 3 : 'Geeks' }
# Deleting a key
# using pop() method
pop_ele = Dict .pop( 1 )
print ( 'Dictionary after deletion: ' + str ( Dict ))
print ( 'Value associated to poped key is: ' + str (pop_ele))


输出:

Dictionary after deletion: {3: 'Geeks', 'name': 'For'}Value associated to poped key is: Geeks

使用popitem()方法

popitem()返回并从字典中删除任意元素(键、值)对。

Python3

# Creating Dictionary
Dict = { 1 : 'Geeks' , 'name' : 'For' , 3 : 'Geeks' }
# Deleting an arbitrary key
# using popitem() function
pop_ele = Dict .popitem()
print ( "Dictionary after deletion: " + str ( Dict ))
print ( "The arbitrary pair returned is: " + str (pop_ele))


输出:

Dictionary after deletion: {3: 'Geeks', 'name': 'For'}The arbitrary pair returned is: (1, 'Geeks')

使用clear()方法

可以通过使用一次删除字典中的所有条目 清除() 方法

Python3

# Creating a Dictionary
Dict = { 1 : 'Geeks' , 'name' : 'For' , 3 : 'Geeks' }
# Deleting entire Dictionary
Dict .clear()
print ( "Deleting Entire Dictionary: " )
print ( Dict )


输出:

Deleting Entire Dictionary: {}

字典方法

方法 描述
复印件() 方法返回字典的浅拷贝。
清除() clear()方法从字典中删除所有项。
pop() 从具有给定键的字典中移除并返回元素。
popitem() 从字典中删除任意键值对,并将其作为元组返回。
得到() 这是访问密钥值的常规方法。
你的名字。价值观() 返回给定字典中所有可用值的列表。
str() 生成字典的可打印字符串表示形式。
更新() 将dictionary dict2的键值对添加到dict
setdefault() Set dict[key]=默认值,如果dict中还没有key
钥匙() 返回dictionary dict的键列表
项目() 返回dict(键、值)元组对的列表
有_key() 如果在dictionary dict中输入,则返回true,否则返回false
fromkeys() 创建一个新字典,其中键来自seq,值设置为value。
类型() 返回传递的变量的类型。
cmp() 比较两个dict的元素。

最近关于Python字典的文章

https://youtu.be/z7z_e5

更多关于Python字典的视频: Python字典集2 Python字典集3

字典程序

有用的链接

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享