Page 1 of 1

[solved] can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 11:51 am
by Kia
I tried this simple code expecting 1,2,3:

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest:
    vbox:
        for i in dutst:
            text i
but it gives me 1,3,2
can somebody test it and make sure it's not just my computer doing it?

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 11:59 am
by drKlauz
Python uses hashtables for dicts, while integers may appear in 1-2-3 order, strings and pretty much any other thing are in random order.
Use dutst_order list ["1","2","3"], iterate over it then read actual value from dict.
Or try OrderedDict, tho not sure how good it behave with RenPy save/rollback systems.

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 12:10 pm
by isobellesophia
I dont think it is related to this one...

https://www.renpy.org/doc/html/language ... ion-syntax

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 3:04 pm
by hell_oh_world
Kia wrote: Sun Sep 01, 2019 11:51 am I tried this simple code expecting 1,2,3:

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest:
    vbox:
        for i in dutst:
            text i
but it gives me 1,3,2
can somebody test it and make sure it's not just my computer doing it?
Quite not sure what you're trying to iterate over. If its the keys then you could do it like this.

Code: Select all


for i in dutst.keys(): # use keys() method to iterate over the keys, or if you're iterating over the dictionary's values use values() method instead.
   text i

I think the dictionary should appear the way it was declared. So it should display 1, 2, 3. But I'm not quite sure though if python sorts the dictionary afterhand. Because if it will be sorted and the basis of sorting is a string then it will be based on ascii characters rather than on numerical sorting. So weird things might happen. I'm not quite sure though.

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 3:45 pm
by Kia
I'm pretty sure it's not python, the same code in python returns 1, 2, 3
it seems that something happens while storing a dictionary in renpy

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 4:48 pm
by IrinaLazareva
Hmm, It's even more strange if the numbers are replaced with letters... (I've also tested in old versions, the same result.)

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 4:54 pm
by Alex
Kia wrote: Sun Sep 01, 2019 3:45 pm I'm pretty sure it's not python, the same code in python returns 1, 2, 3
it seems that something happens while storing a dictionary in renpy
My two cents:
- it's not obvious what you want to show, 'cause dict has a pairs of key-value;
- where did you test this code that gave you 1,2,3? In docs for python 2.6 it said 'It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).' - https://docs.python.org/2.6/tutorial/da ... ctionaries

So, try

Code: Select all

default dutst = {
        "1": 1,
        "2": 2,
        "3": 3,
    }
screen dictest():
    hbox:
        align (0.5, 0.1)
        spacing 50
        vbox:
            for i in sorted(dutst.keys()):
                text "\'{}\' - {}".format(i, dutst[i])
            
        vbox:
            for i in dutst.keys():
                text "\'{}\' - {}".format(i, dutst[i])
            
            

# The game starts here.

label start:
    "..."
    show screen dictest
    "...  ..."
    "..."
    "?!" 

Re: can somebody test this code and tell me the result please

Posted: Sun Sep 01, 2019 7:55 pm
by trooper6
Dicts are unordered in Python, so those parts can come in any order.
If you want ordered, use an ordered dict.
https://www.geeksforgeeks.org/ordereddict-in-python/

Re: can somebody test this code and tell me the result please

Posted: Mon Sep 02, 2019 12:29 am
by Kia
I see, I tested it with python 3.
wasn't there something about renpy migrating to python 3 at version 7.0?

now the question is: is there any way to get the original order without adding an index number to re order with?

Re: can somebody test this code and tell me the result please

Posted: Mon Sep 02, 2019 1:44 am
by trooper6
Did you follow the link I provided? All you have to do have your data ordered is to used an ordered dict rather than a dict.
Also I have no idea when/if Renpy will migrate to Python 3.

Re: can somebody test this code and tell me the result please

Posted: Mon Sep 02, 2019 3:22 am
by drKlauz
They changed dicts to be sorted-by-insertion-order in Python 3.7 i think, maybe you tested it there. RenPy uses 2.7 and frankly i don't think PyTom will upgrade Python version any soon as it will require to drop legacy support for some stuff.

Re: can somebody test this code and tell me the result please

Posted: Mon Sep 02, 2019 6:08 am
by Kia
trooper6 wrote: Sun Sep 01, 2019 7:55 pm Did you follow the link I provided? All you have to do have your data ordered is to used an ordered dict rather than a dict.
I did, not as pretty as I like the code to be, requires a python block, importing something and creating an extra variable to pass it to a class. it does work though.
but if that's what it takes, I prefer using lists and putting the key in it as another value instead, avoiding the dictionaries all together. makes for a less messy code.
edit: I forgot to thank you all for your input on the topic. Thank you :wink:

Re: [solved] can somebody test this code and tell me the result please

Posted: Mon Sep 02, 2019 7:30 am
by strayerror
Just a small addition to note that for the test case a dict isn't a good choice of structure; neither the unique key nor lookup properties of a dict at being used, yet there is a desire for the data to remain ordered, if that's the extent of it then as someone already noted a simple list would solve your problem (especially since only the keys are being iterated over).

One solution that may fit this bill but I don't see mentioned is that of a tuple of tuples, which would fit some use-cases without multiple lists and lookups to retain ordering, I don't know if that will apply here, but here's an example:

Code: Select all

default data = (('1', 1, 'foo'),
                ('2', 2, 'bar'),
                ('3', 3, 'qux'))

screen test():
    vbox:
        spacing 10
        for a, b, c in data:
            hbox:
                spacing 10
                text a
                text '[b]' # interpolate to convert int
                text c
This allows use of as many or as few values per "row" as needed. The tuples could also be substituted for lists if the structure needs to be mutable.