LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2012, 07:14 AM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
Question using iteritems() with dictionary


I've got strange error with iteritems(). why is it so?


Code:
>>> class test:
	def __init__(self,**marks):
		self._marks = marks
	def getMarks(self):
		for key,value in self._marks.iteritems():
			print (self._marks(key,value))

			
>>> t1=test(sub1=99,sub2=98)
>>> t1.getMarks()
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    t1.getMarks()
  File "<pyshell#10>", line 5, in getMarks
    for key,value in self._marks.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
but when i did items(), it works well


Code:
__dict = {'sub1':99,'sub2':98}

>>> for key,val in __dict.items():
	print(key,val)

	
sub2 98
sub1 99
Also i need to print the result in the same order as in the input i.e.
sub1 99
sub2 98.
how can I do it?

Last edited by jone kim; 05-03-2012 at 07:24 AM. Reason: forget to add this query
 
Old 05-03-2012, 07:17 AM   #2
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
Question

Quote:
Originally Posted by jone kim View Post
I've got strange error with iteritems(). why is it so?


Code:
>>> class test:
	def __init__(self,**marks):
		self._marks = marks
	def getMarks(self):
		for key,value in self._marks.iteritems():
			print (self._marks(key,value))

			
>>> t1=test(sub1=99,sub2=98)
>>> t1.getMarks()
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    t1.getMarks()
  File "<pyshell#10>", line 5, in getMarks
    for key,value in self._marks.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
but when i did items(), it works well


Code:
__dict = {'sub1':99,'sub2':98}

>>> for key,val in __dict.items():
	print(key,val)

	
sub2 98
sub1 99
Also i need to print the result in the same order as in the input i.e.
sub1 99 and after that
sub2 98.
how can I do it?

Last edited by jone kim; 05-03-2012 at 07:23 AM.
 
Old 05-06-2012, 01:47 PM   #3
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 360

Rep: Reputation: 199Reputation: 199
Which Python version is that? In Python 3 dict object only has .items() method which returns a so called view object.

Quote:
Originally Posted by jone kim View Post
Also i need to print the result in the same order as in the input i.e.
sub1 99
sub2 98.
how can I do it?
OrderedDict from collections module will help you here.
 
1 members found this post helpful.
  


Reply

Tags
python311



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggest a good offline dictionary-database for gnome-dictionary. abhijeetnayak Linux - Software 1 07-05-2009 05:17 PM
Better dictionary for gnome-dictionary? General Linux - Software 4 08-21-2006 10:39 PM
Dictionary michshelly LQ Suggestions & Feedback 2 04-26-2006 09:20 PM
dictionary Kdr Kane Programming 3 03-29-2005 02:38 AM
need dictionary tornado Linux - Software 3 09-25-2003 09:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration