Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81980

dict turn to list unexpected at runtime???

From "telnetgmike@gmail.com" <telnetgmike@gmail.com>
Subject dict turn to list unexpected at runtime???
Date 2014-12-05 15:01 +0800
Newsgroups comp.lang.python
Message-ID <mailman.16610.1417762972.18130.python-list@python.org> (permalink)

Show all headers | View raw


Why the following code gives me errors??? And why the print statement run 2 times? I'll be appreciated your helps, thanks,
addrnum_dict = {'a':1,'b':2}
def orderaddrtimes():
    global addrnum_dict
    print type(addrnum_dict)

    addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = True)
    #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: t[0]))
if __name__ == '__main__':
    kinds = ["a","b"]
    for tmp_kind in kinds:
        orderaddrtimes()

######################
errors:
python aaa.py 
<type 'dict'>
<type 'list'>
Traceback (most recent call last):
  File "aaa.py", line 16, in <module>
    orderaddrtimes()
  File "aaa.py", line 11, in orderaddrtimes
    addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = True)
AttributeError: 'list' object has no attribute 'iteritems'

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

dict turn to list unexpected at runtime??? "telnetgmike@gmail.com" <telnetgmike@gmail.com> - 2014-12-05 15:01 +0800
  Re: dict turn to list unexpected at runtime??? Dan Sommers <dan@tombstonezero.net> - 2014-12-05 07:15 +0000

csiph-web