[转载] python字典查询功能_Python中的字典功能

参考链接: Python中的字典dictionary方法 (cmp(), len(), items()…)

python字典查询功能

 Let's check out some important functions that are quite helpful when we are playing around with dictionaries in python. 

  让我们看看一些重要的函数,这些函数在处理python中的字典时非常有帮助。  

  len() (len()) 

 As you might have already guessed, it gives the number of elements stored in the dictionary or you can say just the number of keys in it. 

  您可能已经猜到了,它给出了存储在字典中的元素数,或者您只能说其中的键数。  

 >>> print(len(myDictionary)); 

  5  

  5  

  明确() (clear()) 

 If we ever need to delete all elements of the dictionary using the del keyword, for each key value, that would be quite troublesome. Hence clear() function makes emptying a dictionary, a single line task. 

  如果我们曾经需要使用del关键字删除字典的所有元素,那么对于每个键值来说 ,这将很麻烦。 因此, clear()函数使清空字典(单行任务)成为可能。  

 >>>myDictionary.clear()

>>> print (myDictionary); 

  {}  

  {}  

  values() (values()) 

 Suppose you don't want to access the keys while displaying the value stored in the dictionaries, then values() function can be used. This function will show all the values stored in the dictionary. 

  假设您不想在显示字典中存储的值时访问键 ,则可以使用values()函数。 此功能将显示存储在词典中的所有值。  

 >>> print (myDictionary.values()); 

  {"Element-1", "Element-2", "Element-3", "Element-4", "Element-5"}  

  {“ Element-1”,“ Element-2”,“ Element-3”,“ Element-4”,“ Element-5”}  

  keys() (keys()) 

 This function is opposite to the function values(). As the name suggests, in case you want to view only the keys for a dictionary, then you can use the keys() function. 

  此函数与函数values()相反。 顾名思义,如果只想查看字典的键 ,则可以使用keys()函数。  

 >>>print  (myDictionary.keys()); 

  {"Key-1", "Key-2", "Key-3", "Key-4", "Key-5"}  

  {“ Key-1”,“ Key-2”,“ Key-3”,“ Key-4”,“ Key-5”}  

  items() (items()) 

 In case you want to display both, keys and values with a representation, where both are well mapped, then use the items() method. 

  如果要同时显示键和值及其表示形式,并且二者之间的映射关系很好,则可以使用items()方法。  

 >>> print (myDictionary.items()); 

  {('Key-1': 'Element-1'), ('Key-2': 'Element-2'), ('Key-3': 'Element-3'), ('Key-4': 'Element-4'), ('Key-5': 'Element-5')}  

  {('Key-1':'Element-1'),('Key-2':'Element-2'),('Key-3':'Element-3'),('Key-4': 'Element-4'),('Key-5':'Element-5')}  

  __包含__ (__contains__) 

 To check if a particular key exists in the dictionary, this function can be used. If the key that you are looking for exists then it returns True, otherwise False. 

  要检查字典中是否存在特定键 ,可以使用此功能。 如果要查找的键存在,则返回True ,否则返回False 。  

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

print(myDictionary.__contains__('Key-2')); 

  True  

  真正  

  cmp(): (cmp():) 

 In case you ever need to compare two dictionaries, cmp() function can be used to do so. It can return 3 possible values, i.e., 1, 0 or -1. cmp() function was available in Python 2.x but is not available in Python 3.x but we can easily define this function and use it.If both dictionaries are equal, then 0. If second have greater elements than first, then -1 and 1 for the reverse. It can be used like 

  如果您需要比较两个字典,则可以使用cmp()函数进行比较。 它可以返回3个可能的值,即1、0或-1。 cmp()函数在Python 2.x中可用,但在Python 3.x中不可用,但我们可以轻松定义此函数并使用它。如果两个字典相等,则为0。如果second的元素数大于first,则- 1和1相反。 它可以像  

 >>> x = {1: 1, 2:2, 3:3}

>>> y = {1:1, 2:2, 3:3}

>>> print (cmp(x, y)) 

  0  

  0  

 Since both are same, hence the output is 0. 

  由于两者相同,因此输出为0 。  

 Now let's add one an extra element to x. 

  现在让我们向x添加一个额外的元素。  

 >>> x[4] = 4

>>> cmp(x, y) 

  1  

  1个  

 Now that x is having more elements, hence the output is 1. Also, if we compare y with x, then 

  现在x具有更多元素,因此输出为1 。 另外,如果我们将y与x进行比较,则  

 >>> cmp(y, x) 

  -1  

  -1  

 The output becomes -1, because y has less elements. 

  输出变为-1 ,因为y元素较少。  

 cmp is not supported in Python 3 

  Python 3不支持cmp  

 

  翻译自: https://www.studytonight.com/python/functions-for-dictionary

 

 python字典查询功能

上一篇:2.14


下一篇:1023 组个最小数 (20 分)