python中的字典_Python中的字典

python中的字典

In this tutorial, we will learn what a Dictionary is? How it can be used? and some useful functions used for manipulating Dictionary.

在本教程中,我们将学习什么是字典? 如何使用? 以及一些用于操作Dictionary的有用功能。

Dictionaries are much like lists with an extra parameter called keys. Recall, how in lists and strings, we used the index as the parameter to access each element of the string/list. The main differentiating factor between a list and a dictionary would be, that instead of the index we use keys to access the elements of a dictionary (or values to access keys, works both ways).

字典很像列表,带有一个额外的参数,称为keys 。 回想一下,如何在列表和字符串中使用索引作为参数来访问字符串/列表的每个元素。 列表和字典之间的主要区别在于,我们使用而不是索引来访问字典的元素(或访问键的值双向起作用)。

Also, unlike an index, keys can be of any data type varying from integer to string. This makes them more flexible to use.

而且,与索引不同,键可以是从整数到字符串的任何数据类型。 这使它们使用起来更加灵活。

创建字典 (Creating a Dictionary)

Since we have flexibility in providing the key for each element in the dictionary, we will have to define each key explicitly. Below we have a dictionary in tabular format. For each element in the dictionary we have a key linked to it.

由于我们可以灵活地为字典中的每个元素提供键,因此我们必须明确定义每个键。 下面我们有一个表格格式的字典。 对于字典中的每个元素,我们都有一个链接到它的键。

KeyValue
Key-1Element-1
Key-2Element-2
Key-3Element-3
Key-4Element-4
Key-5Element-5
键1 元素1
键2 元素2
键3 元素3
键4 元素4
键5 元素5

A dictionary in python can be created as:

python中的字典可以创建为:

>>> myDictionary = {'Key-1': 'Element-1', 'Key-2': 'Element-2', 'Key-3': 'Element-3', 'Key-4': 'Element-4'}

Notice the curly braces that are used here, unlike square braces in the list. Here Key-1, Key-2... are the keys for Element-1, Element-2... respectively. Therefore, if you want to access any element of a dictionary, you should know the key for that element. For example, to access element of Key-3, then just use,

请注意此处使用的花括号,与列表中的方括号不同。 这里的Key-1Key-2 ...分别是Element-1Element-2 ...的键。 因此,如果要访问字典中的任何元素,则应知道该元素的键。 例如,要访问Key-3的元素,只需使用,

>>>print( myDictionary['Key-3']);

'Element-3'

'元素3'

Also, in a dictionary, each element must have a unique key, since a key is used to uniquely identity each element of the dictionary, however, the reverse is not true, which means that elements can be repeated, but key must be unique.

同样,在字典中,每个元素必须具有唯一的键,因为键用于唯一地标识字典中的每个元素,但是,相反的说法并不正确,这意味着可以重复元素,但是键必须是唯一的。

带有整数键的字典: (Dictionary with integer keys:)
>>> integerDictionary = {10: "C++", 20: "Java", 30: "Python", 40: "Ruby", 50: "C#", 60: "Perl"}
>>> print (integerDictionary[30]);

"Python"

“Python”

以字符串为键的字典: (Dictionary with string as keys:)
>>> identity = {"name": "StudyTonight", "type": "Educational", "link": "https://studytonight.com", "tag": "Best place to learn"}
>>> print (identity['name'] + ": " + identity['tag']);

StudyTonight: Best place to learn

StudyTonight:最佳学习场所

To create an empty dictionary, do the following:

要创建一个空字典,请执行以下操作:

>>> emptyList = {}

The above line of code successfully initialized an empty dictionary. We can easily add elements to an empty dictionary after its initialization. Suppose you want to add Delhi with key India to your dictionary, then do it like,

上面的代码行成功初始化了一个空字典。 初始化后,我们可以轻松地将元素添加到空字典中。 假设您想在字典中添加带有印度关键字的德里 ,然后像这样进行操作,

>>> emptyList["India"] = "Delhi"

And this element will get appended to the dictionary.

并且此元素将被添加到字典中。

>>> print(emptyList);

{"India": "Delhi"}

{“印度”:“德里”}

访问字典的元素 (Accessing elements of a dictionary)

Elements stored in a dictionary can be accessed just like lists in python, i.e, using the for loop. However, while iterating over each element, we will get the key and not the value of the element, therefore, to access the value of the element, we have to use the key just like index, For example: myDictionary[key].

可以像访问python中的列表一样访问字典中存储的元素,即使用for循环。 但是,在遍历每个元素时,我们将获得键而不是元素的值,因此,要访问元素的值,我们必须像索引一样使用键,例如: myDictionary[key]

for i in myDictionary:
    print ("Key: " + i + " and Element: " + myDictionary[i]);

Key: Key-1 and Element: Element-1 Key: Key-2 and Element: Element-2 Key: Key-3 and Element: Element-3 Key: Key-4 and Element: Element-4 Key: Key-5 and Element: Element-5

键:键1和元素:元素1键:键2和元素:元素2键:键3和元素:元素3键:键4和元素:元素4键:键5和元素:Element-5

删除字典中的元素 (Deleting element(s) in a dictionary)

Elements can be deleted using del keyword, which is similar to how its done in a list. For example, considering our website details dictionary,

可以使用del关键字删除元素,这与列表中的操作类似。 例如,考虑我们的网站详细信息字典,

>>> identity = {"name": "StudyTonight", "type": "Educational", "link": "http://studytonight.com", "tag": "Best place to learn"}

If we want to delete the link key and the value associated to it, then

如果我们要删除链接键及其关联的值,则

>>> del identity["link"]

will delete that element.

将删除该元素。

>>> print (identity);

{"name": "StudyTonight", "type": "Educational", "field": "tag": "Best place to learn"}

{“名称”:“ StudyTonight”,“类型”:“教育性”,“领域”:“标签”:“最佳学习场所”}

将元素追加到字典 (Appending element(s) to a dictionary)

Suppose you want to add an extra element to your already initialized list which has elements, then, all you have to do is:

假设您想向已初始化的列表中添加一个包含元素的额外元素,那么,您要做的就是:

>>> identity["email": "we@studytonight.com"]

And it will be added to the dictionary.

它将被添加到字典中。

>>> print (identity);

{"name": "StudyTonight", "type": "Educational", "tag": "Best place to learn", "email": "we@studytonight.com"}

{“名称”:“ StudyTonight”,“类型”:“教育性”,“标签”:“最佳学习场所”,“电子邮件”:“ we@studytonight.com”}

更新字典中的现有元素 (Updating existing element(s) in a dictionary)

The update() function is used for merging two dictionaries into one. The common values of both the lists get overwritten by the latter dictionary. For example, let's assume that there is another dictionary containing the list of the available courses on StudyTonight, along with the list used in the example above.

update()函数用于将两个词典合并为一个。 两个列表的公共值都被后一个字典覆盖。 例如,我们假设还有另一本词典,其中包含StudyTonight上可用课程的列表,以及上面示例中使用的列表。

So now we have 2 lists - identity and courseAvail

所以现在我们有2个列表- identitycourseAvail

>>> courseAvail = {"Java": "Full-course", "C/C++": "Full-course", "DBMS": "Full-course"}

Suppose we want to copy all elements of courseAvail to the list identity, then we just have to do th following:

假设我们要将courseAvail所有元素都courseAvail到列表identity ,那么我们只需执行以下操作:

>>> identity.update(courseAvail)

Note: In this case dictionary identity will get updated, and there would be no effect on dictionary courseAvail.

注意:在这种情况下,字典identity将被更新,并且对字典courseAvail

翻译自: https://www.studytonight.com/python/dictionaries-in-python

python中的字典

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值