Accessing dictionary variable data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    Accessing dictionary variable data

    The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

    Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

    Thanks in advance
    PSB
    Code:
    >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
    >>> print dict1
    {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
    >>>
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by psbasha
    The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

    Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

    Thanks in advance
    PSB
    Code:
    >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
    >>> print dict1
    {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
    >>>
    the typical way to do it is by sorting the keys
    Code:
    >>> for keys in sorted(dict1.keys()):
    ...  print keys, dict1[keys]
    ...
    E1 [1, 2, 6, 5]
    E2 [2, 3, 7, 6]
    E3 [3, 4, 8, 7]
    E4 [5, 6, 10, 9]
    E5 [6, 7, 11, 10]
    E6 [7, 8, 12, 11]

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      In this example the data is available in a order.But if the data is available in this order.

      dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

      Thanks
      PSB

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by psbasha
        In this example the data is available in a order.But if the data is available in this order.

        dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

        Thanks
        PSB
        Dictionaries are NOT ordered collections. They are ramdomized for faster retreival (hashing).

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by psbasha
          In this example the data is available in a order.But if the data is available in this order.

          dict1= {E2":[2,3,7,6],"E3":[3,4,8,7],"E1":[1,2,6,5],","E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11],"E3":[3,4,8,7]}

          Thanks
          PSB
          pls explain clearly. do you want to sort the keys ? or the dictionary values?
          sort the keys means you want E1, then E2 and so on. Sort the values means for example, you want to sort [2,3,7,6] to become [2,3,6,7]

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by psbasha
            The following example shows the storing of the data and accessing the data from dictionary variable.The accessing of dictionary variable data is not a orderas what we are storing.

            Is my understanding is correct?.Is it possible to retrievethe dictiondary variable data in a order,as what we store?

            Thanks in advance
            PSB
            Code:
            >>> dict1= {"E1":[1,2,6,5],"E2":[2,3,7,6],"E3":[3,4,8,7],"E4":[5,6,10,9],"E5":[6,7,11,10],"E6":[7,8,12,11]}
            >>> print dict1
            {'E5': [6, 7, 11, 10], 'E4': [5, 6, 10, 9], 'E6': [7, 8, 12, 11], 'E1': [1, 2, 6, 5], 'E3': [3, 4, 8, 7], 'E2': [2, 3, 7, 6]}
            >>>
            If you want to retrieve the data in the same order it was stored, you can use lists or tuples.
            Code:
            >>> keyList = ["E1", "E2", "E3", "E4"]
            >>> dataList = [[1,2,6,5],[2,3,7,6],[3,4,8,7],[5,6,10,9]]
            >>> for i, key in enumerate(keyList):
            ... 	print '%s = %s' % (key, dataList[i])
            ... 	
            E1 = [1, 2, 6, 5]
            E2 = [2, 3, 7, 6]
            E3 = [3, 4, 8, 7]
            E4 = [5, 6, 10, 9]
            >>>
            A dictionary stores data randomly. Data items can be appended to lists as they are acquired. Retrieving dictionary data from a sorted key list will not necessarily match the order of data acquisition.

            Is this what you are trying to do?

            Comment

            • psbasha
              Contributor
              • Feb 2007
              • 440

              #7
              Hi,

              Thanks for the reply

              Yes,You are right.

              PSB

              Comment

              Working...