Jump to content

Use OrderedDict in Python 27


Recommended Posts

  • Active Member

Hello Metin2Dev users, this topic is meant to teach you how to use OrderedDict in Python

Requirements:

  • python 2.7

 

A little example with the default dictionary

this_is_my_dict = {"key1" : 1,"key2" : 2,"key3" : 3,"key4" : 4}

for key in this_is_my_dict:

    print key

 

The result of this little example will be something like th is:

key3

key2

key1

key4

They are not in order (1,2,3,4)

So if you want them to be in order the code should be like this:

from collections import OrderedDict

this_is_my_dict=OrderedDict()

this_is_my_dict["key1"]=1

this_is_my_dict["key2"]=2

this_is_my_dict["key3"]=3

this_is_my_dict["key4"]=4

for key in this_is_my_dict:

    print key

And now the output will be this:

key1

key2

key3

key4

Link to comment
Share on other sites

  • Active Member

It's an ordered dictionary in python, normally when you loop through a dict the keys inside the dict are not being taken in order.

Link to comment
Share on other sites

dict does not store the original order of the item that were stored in it. (dict is pretty much like std::map in c++) (http://stackoverflow.com/questions/11274978/are-c-stdmapstring-string-ordered <- this will help you a bit , it's a bit off topic but if you are a c++ dev it can help you understand)

so python offer another class which is the collections.OrderedDict which quite useful. for iteration.

say :

list(dict()) , will return a list which contain the keys of the dict() without the original order.

list( collections.OrderedDict()) , will return a list whish contain the keys of the OrderedDict in their insertion order.

and as always it is useful for iteration. and some other rare cases.

there are other choices , but this is the first one google will point out.

Link to comment
Share on other sites

I would prefer to work more nice.

List = [ ]
List = [
	[[ui.TextClass, 0], [0, 0], [x, y], [["SetDefaultFontName", [""]]], []],
	[[ui.ImageClass, ""], [0, 0], [x , y], [["LoadImage", ["image"]]], ["movable", "float"]],
]
	def SetText(self, index, msg = None):			
		self.List[0].SetText("Text" + msg)
		self.List[1].Show()

 

	self.List = [
		{"name": "A", "number": 1},
		{"name": "B", "number": 2},
	]
		
	self.Select = {}
	self.Select["A"] = self.Loader.Button(self.Board, 'A', '', 15, 50, self.Main, 'A', 'A', 'A')
	self.Select["B"] = self.Loader.Button(self.Board, 'B', '', 15, 50*2, self.Main, 'B', 'B', 'B')
		
	for i in xrange(len(self.List)):
		self.Select[self.List[i]["name"]].Show()

 

Link to comment
Share on other sites

29 minutes ago, VegaS said:

I would prefer to work more nice.


List = [ ]
List =
[
	[[ui.TextClass, 0], [0, 0], [x, y], [["SetDefaultFontName", [""]]], []],
	[[ui.ImageClass, ""], [0, 0], [x , y], [["LoadImage", ["image"]]], ["movable", "float"]],
]

	def SetText(self, index, msg = None):			
		self.List[0].SetText("Text" + msg)
		self.List[1].Show()

 


		self.List = [
			{"name": "A", "number": 1},
			{"name": "B", "number": 2},
		]
		
		self.Select = {}
		self.Select["A"] = self.Loader.Button(self.Board, 'A', '', 15, 50, self.Main, 'A', 'A', 'A')
		self.Select["B"] = self.Loader.Button(self.Board, 'B', '', 15, 50*2, self.Main, 'B', 'B', 'B')
		
		for i in xrange(len(self.List)):
			self.Select[self.List[i]["name"]].Show()

you used  a multidimensional array aka a list of lists , the first and second line of SetText will result in Attribute error exception to be raised.

your method might be correct depending on how you do it , but it will result in you having to wast more time.

for the List var you could use a tuple as it is a immutable (it does not change no matter what).

ex.

List = [ ]
List =
[
	[(ui.TextClass, 0), (0, 0), (x, y), (["SetDefaultFontName", [""]]), ()],
	[(ui.ImageClass, ""), (0, 0), (x , y), (["LoadImage", ["image"]]), ("movable", "float")],
]

metin2 does have it's own methods for dealing with UI , i highly recommend using it. (there are already over 50 example of it in uiscript and the locale dir of each client).

  • Love 2
Link to comment
Share on other sites

7 hours ago, Night said:

you used  a multidimensional array aka a list of lists , the first and second line of SetText will result in Attribute error exception to be raised.

your method might be correct depending on how you do it , but it will result in you having to wast more time.

for the List var you could use a tuple as it is a immutable (it does not change no matter what).

ex.


List = [ ]
List =
[
	[(ui.TextClass, 0), (0, 0), (x, y), (["SetDefaultFontName", [""]]), ()],
	[(ui.ImageClass, ""), (0, 0), (x , y), (["LoadImage", ["image"]]), ("movable", "float")],
]

metin2 does have it's own methods for dealing with UI , i highly recommend using it. (there are already over 50 example of it in uiscript and the locale dir of each client).


:D In much you were right about:

your method might be correct depending on how you do it , but it will result in you having to wast more time.

PS: For those who want another example to work more efficiently, feel free to forward your ideas.

List = {
	"selected"  : {
		0 : 0,
		1 : 0
	}
}
	self.m_nTCheckBox = {
		0	:	[uiModuleTest.CheckTable(self, x, x, lambda x = 0: self.SetArgument(x)), 0, self.SetText(x, y, "A")],
		1	:	[uiModuleTest.CheckTable(self, x, y, lambda x = 1: self.SetArgument(x)), 0, self.SetText(x, y, "B")],
	}	
	def SetArgument(self, x):
		for index in xrange(len(List["selected"])):
			if x == index:
				if List["selected"][x] > 0:
					self.m_nTCheckBox[x][0].AppendCheck(false)
					List["selected"][x] = 0
				else:
					self.m_nTCheckBox[x][0].AppendCheck(true)
					List["selected"][x] = 1

			chat.AppendChat(chat.CHAT_TYPE_INFO, "<Index selected> <Argument %d> <Check %d>" % int(x), int(List["selected"][index]))

 

  • Love 1
Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.