python字典排序
总览 (Overview)
A dictionary in python is unordered, changeable and indexed. It is unordered because it does not keep track of the order of data inserted. It is changeable because you can change the values of keys. It is indexed because it retrieves data based on the key meaning each key only has one value.
python中的字典是无序的,可变的且已建立索引。 它是无序的,因为它无法跟踪插入的数据的顺序。 它是可变的,因为您可以更改键的值。 它被索引是因为它基于密钥检索数据,这意味着每个密钥只有一个值。
新字典 (New Dictionary)
To create a dictionary you can use curly braces or the dict() constructor.
要创建字典,您可以使用花括号或dict()构造函数。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}otherDictionary = dict(name="Bob", age=21, email="bob@emai.com")
It is possible to have empty string and None value as keys and values. It is not possible to have duplicate keys.
可能有空字符串和无值作为键和值。 不可能有重复的密钥。
读 (Read)
To get values from the dictionary you need to provide the key inside square brackets or use the get() method
要从字典中获取值,您需要在方括号中提供键或使用get()方法
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}print(dictionary["name"])
>> Bob
print(dictionary.get("age"))
>> 21
更新资料 (Update)
To change the value of the key you need to reference the key similar to how you would reference the index of a list.
要更改键的值,您需要引用键,类似于引用列表索引的方式。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dictionary["name"] = "Mike"
print(dictionary["name"])
>> Mike
迭代 (Iteration)
You can iterate through the keys, values or both using a for loop.
您可以使用for循环遍历键,值或两者。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}for key in dictionary:
print(key)
>> name
>> age
>> emailfor key in dictionary:
print(dictionary[key])
>> Bob
>> 21
>> bob@email.comfor key, value in dictionary.items:
print(key, value)
>> name Bob
>> age 21
>> email bob@email.com
长度 (Length)
To check the length of the dictionary you can use the len() method
要检查字典的长度,可以使用len()方法
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
print(len(dictionary))
>> 3
创造 (Create)
To create a new key and value in an existing dictionary you can reference the new key and give it a value.
要在现有字典中创建新键和值,您可以引用新键并为其赋值。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dictionary["job"] = "Teacher"
删除 (Delete)
To delete a key and value you can use the pop() method by providing it the key.
要删除键和值,可以通过提供键来使用pop()方法。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dictionary.pop("email")
print(dictionary)
>> {'age': 21, 'age': 21}
In python version 3.7 and above you can use the popitem() method to delete the most recent key and value.
在3.7和更高版本的python中,您可以使用popitem()方法删除最新的键和值。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dictionary["job"] = "teacher"
dictionary.popitem()
print(dictionary)
>> {'age': 21, 'name': 'Bob', 'email': 'bob@email.com'}
To clear the entire dictionary but keep the data structure you can use the clear() method.
要清除整个词典但保留数据结构,可以使用clear()方法。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dictionary.clear()
print(dictionary)
>> {}
复制 (Copy)
To create a copy of a dictionary you can use the copy method or pass the copy method to the dict() constructor.
要创建字典的副本,可以使用copy方法或将copy方法传递给dict()构造函数。
dictionary = {
"name": "Bob",
"age": 21,
"email": "bob@email.com"
}
dict1 = dictionary.copy()
dict2 = dict(dictionary)
嵌套词典 (Nested Dictionary)
It is possible to have a dictionary within a dictionary by having a key and another dictionary as a value.
通过将键和另一个字典作为值,可以在字典中包含一个字典。
students = {
"One" : {
"name": "Bob",
"age": "10"
},
"Two" : {
"name": "Jack",
"age": "9"
},
}
结论 (Conclusion)
Pythons dictionary is similar to Javas Hashmap where both supports key value pairs, does not have duplicate keys, are unordered, changeable and indexed. You would use dictionaries when you want to associate certain values with keys. Because dictionaries are hashed it makes storing and retrieving values faster than an ordered list.
Pythons字典类似于Javas Hashmap,在Javas Hashmap中,它们都支持键值对,没有重复的键,并且无序,可更改且已建立索引。 当您想将某些值与键关联时,将使用字典。 由于字典是散列的,因此它使存储和检索值的速度比有序列表更快。
翻译自: https://medium.com/@danliu277/python-dictionary-d0d7f0d2849d
python字典排序