Dictionary usage in Python

From , 5 Years ago, written in Python, viewed 93 times.
URL https://pastebin.vip/view/69f62956
  1. #!/usr/bin/env python
  2. #
  3. # [SNIPPET_NAME: Dictionaries 101]
  4. # [SNIPPET_CATEGORIES: Python Core]
  5. # [SNIPPET_DESCRIPTION: Basic and not so basic dictionary operations]
  6. # [SNIPPET_AUTHOR: Bruno Girin <brunogirin@gmail.com>]
  7. # [SNIPPET_LICENSE: GPL]
  8.  
  9. # This snippet demonstrates how the basics on dictionaries: how to create, add,
  10. # remove items, get items, iterate, etc.
  11.  
  12. #
  13. # First, let's create simple dictionary. A dictionary (called map in Java hash
  14. # in perl) is similar to a list with the difference that the key doesn't
  15. # have to be an integer, it can be anything.
  16. #
  17. # A dictionary is enclosed in curly brackets and each key is mapped to its
  18. # corresponding value with a colon. So in the dictionary below, we associate
  19. # the key Karmic with the value 9.10 and so on for the 5 pairs.
  20. #
  21. print "Create a simple dictionary"
  22. simpleDict = {"Karmic": "9.10", "Lucid": "10.04", "Hardy": "7.10",
  23.               "Jaunty": "8.10", "Intrepid": "8.04"}
  24. # print it
  25. print simpleDict
  26. #
  27. # Another way to create a dictionary is to zip two lists containing the keys
  28. # and values in the same order to create a list of tuples, which we can then
  29. # pass to the dict() method to create a dictionary.
  30. #
  31. myKeys = ['Feisty', 'Edgy', 'Dapper']
  32. myValues = ['7.04', '6.10', '6.06']
  33. otherDict = dict(zip(myKeys, myValues))
  34. print otherDict
  35. #
  36. # Interrogate the dictionary. It works exactly the same as with a list, with the
  37. # exception that the key is no longer an integer.
  38. #
  39. print "\nInterrogate the dictionary"
  40. # get for value for key Jaunty
  41. print simpleDict['Jaunty']
  42. # get the length of the dictionary
  43. print len(simpleDict)
  44. # check if the dictionary contains the key Lucid
  45. print 'Lucid' in simpleDict
  46. print 'Breezy' in simpleDict
  47. #
  48. # Modify the dictionary
  49. #
  50. print "\nModify the dictionary"
  51. # add another item
  52. simpleDict['Hoary'] = '5.06'
  53. print simpleDict
  54. # oops! let's sort this out by replacing in place
  55. simpleDict['Hoary'] = '5.04'
  56. print simpleDict
  57. # update the dictionary with mappings from another one
  58. simpleDict.update(otherDict)
  59. print simpleDict
  60. # remove an item from the list (Hardy should not be in the list anymore)
  61. del simpleDict['Hoary']
  62. print simpleDict
  63. #
  64. # Iterate over the dictionary. A dictionary doesn't enforce a natural ordering
  65. # like a list but we can still iterate over it in multiple ways.
  66. # However, note that when you iterate, the order in which the items are
  67. # retrieved is unspecified.
  68. #
  69. print "\nIterate over the dictionary"
  70. print "\nby keys"
  71. for k in simpleDict.keys():
  72.     print k
  73. print "\nby values"
  74. for v in simpleDict.values():
  75.     print v
  76. print "\nby items"
  77. # note the syntax to retrieve the key and value at the same time
  78. for k, v in simpleDict.items():
  79.     print k, '=>', v
  80. #
  81. # More interesting transformations from list to dictionary and vice versa.
  82. # List comprehension allow you to do a lot of interesting stuff, in particular
  83. # tranforming lists into dictionaries and the other way around.
  84. #
  85. print "\nList to dictionary and vice versa"
  86. # First, let's transform our dictinary into a list of tuples
  87. simpleList = [(k, v) for k, v in simpleDict.items() ]
  88. print simpleList
  89. # Create a map from a list with the list's entry as key and the index as value
  90. # This method takes advantage of another way of creating a map, using a
  91. # sequence of tuples, so in practice, we create a tuple for each item in the
  92. # list, create a list from all the tuples using a list comprehension and pass
  93. # it as argument to the dict() function
  94. cityList = ['London', 'Paris', 'New York', 'Tokyo']
  95. cityDict = dict([(x, i) for i, x in enumerate(cityList)])
  96. print cityDict
  97. # Create a map from a number to its square
  98. squareDict = dict([(x, x * x) for x in range(1, 10)])
  99. print squareDict
  100. #//python/2368

Reply to "Dictionary usage in Python"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website