Handling sorted dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • loial

    #1

    Handling sorted dictionaries

    The following code gives the error

    d=sortedmachine s[machine]
    TypeError: list indices must be integers


    What works for the unsorted dictionary does not work for the sorted
    dictionary.
    Can anyone help?


    machinekey = "111111"

    machines = {}
    machines[machinekey]=[1,0,0,0,0,0,0,0 ,0,0,0,0,0]

    machinekey = "222222"

    machines[machinekey]=[0,1,0,0,0,0,0,0 ,0,0,0,0,0]

    ddd=0
    for machine in machines.keys() :
    d=machines[machine]
    print machine
    print d [ddd]

    sortedmachines= sorted(machines )
    for machine in sortedmachines:
    d=sortedmachine s[machine]
    print machine
    print d [ddd]

  • Steven D'Aprano

    #2
    Re: Handling sorted dictionaries

    On Tue, 17 Apr 2007 01:01:55 -0700, loial wrote:
    The following code gives the error
    >
    d=sortedmachine s[machine]
    TypeError: list indices must be integers
    >
    >
    What works for the unsorted dictionary does not work for the sorted
    dictionary.
    Can anyone help?
    The error message you got tells you what the problem is. sortedmachines
    is not a "sorted dictionary". There is no such thing -- dictionaries, also
    known as "hash tables" in other languages, are unsorted. sortedmachines is
    a _list_, just like the error message says, and the index must be an
    integer.

    sortedmachines= sorted(machines )
    sortedmachines is now the sorted _keys_ copied from machines. Try calling
    "print sortedmachines" and looking at what you get.


    for machine in sortedmachines:
    d=sortedmachine s[machine]
    Try this instead:

    d = machines[machine]


    --
    Steven D'Aprano

    Comment

    • filyph

      #3
      Re: Handling sorted dictionaries

      Hello,

      Dictionary items are inordered and can not be sorted. Function
      sorted() returns list of tuples in each is 0. item key from dictionary
      and 1. is value from key-value pair from dict machines. List indices
      must be integers and machine keys are strings. See http://www.python.org/dev/peps/pep-0265/
      about sorting dictionaries.
      You need to know what type is variable in each line of code.
      Dict is not same as list, even when you can get values from they very
      similar way. dict[key] and list[index], key can be everything what can
      be hashed, but index can be only integer.

      I am not english, so I hope you understand me.

      loial napĂ­sal(a):
      The following code gives the error
      d=sortedmachine s[machine]
      TypeError: list indices must be integers
      What works for the unsorted dictionary does not work for the sorted
      dictionary.
      Can anyone help?
      machinekey = "111111"
      machines = {}
      machines[machinekey]=[1,0,0,0,0,0,0,0 ,0,0,0,0,0]
      machinekey = "222222"
      machines[machinekey]=[0,1,0,0,0,0,0,0 ,0,0,0,0,0]
      ddd=0
      for machine in machines.keys() :
      d=machines[machine]
      print machine
      print d [ddd]
      >
      sortedmachines= sorted(machines )
      for machine in sortedmachines:
      d=sortedmachine s[machine]
      print machine
      print d [ddd]

      Comment

      Working...