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


Groups > comp.lang.python > #81979 > unrolled thread

dict turn to list at runtime unexpected!!!

Started bytelnetgmike@gmail.com
First post2014-12-04 22:56 -0800
Last post2014-12-05 12:43 -0500
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  dict turn to list at runtime unexpected!!! telnetgmike@gmail.com - 2014-12-04 22:56 -0800
    Re: dict turn to list at runtime unexpected!!! telnetgmike@gmail.com - 2014-12-04 23:37 -0800
    Re: dict turn to list at runtime unexpected!!! dieter <dieter@handshake.de> - 2014-12-05 08:42 +0100
    Re: dict turn to list at runtime unexpected!!! Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-12-05 12:43 -0500

#81979 — dict turn to list at runtime unexpected!!!

Fromtelnetgmike@gmail.com
Date2014-12-04 22:56 -0800
Subjectdict turn to list at runtime unexpected!!!
Message-ID<e5012cb1-1820-4380-8e84-e8620481cc42@googlegroups.com>
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'

[toc] | [next] | [standalone]


#81985

Fromtelnetgmike@gmail.com
Date2014-12-04 23:37 -0800
Message-ID<4c3d074b-72d5-432d-a856-f6f4a934c90f@googlegroups.com>
In reply to#81979
On Friday, December 5, 2014 2:56:50 PM UTC+8, telne...@gmail.com wrote:
> 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'

https://groups.google.com/forum/#!topic/comp.lang.python/eY7jecLLO48
this man helped me find out the problem.

[toc] | [prev] | [next] | [standalone]


#81987

Fromdieter <dieter@handshake.de>
Date2014-12-05 08:42 +0100
Message-ID<mailman.16614.1417765507.18130.python-list@python.org>
In reply to#81979
telnetgmike@gmail.com writes:

> 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)

"sorted" returns a list.

[toc] | [prev] | [next] | [standalone]


#82010

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-12-05 12:43 -0500
Message-ID<mailman.16631.1417801447.18130.python-list@python.org>
In reply to#81979
On Thu, 4 Dec 2014 22:56:38 -0800 (PST), telnetgmike@gmail.com declaimed
the following:

>Why the following code gives me errors??? And why the print statement run 2 times? I'll be appreciated your helps, thanks,

	It runs twice because your "for tmp_kind" loop calls the function
twice.

	It fails the second time because your sort operation returns a list,
and you bound that list to the name of your dictionary the first time
through.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web