Blaine
Blaine

Reputation: 667

Python fetching a single tuple reversed from a dictionary

I am unsure about the question, but it has me troubled for a brief time now.

I have a dictionary for the pixel dimensions of various paper sizes against 4 seperate PPI's as follows

paperSizes = {
    "A0": {
        "72": {2384, 3370},
        "96": {3179, 4494},
        "150": {4967, 7022},
        "300": {9933, 14043}
    },
    "A1": {
        "72": {1684, 2384},
        "96": {2245, 3179},
        "150": {3508, 4967},
        "300": {7016, 9933}
    },
    "A2": {
        "72": {1191, 1684},
        "96": {1587, 2245},
        "150": {2480, 3508},
        "300": {4960, 7016}
    },
    "A3": {
        "72": {842, 1191},
        "96": {1123, 1587},
        "150": {1754, 2480},
        "300": {3508, 4960}
    },
    "A4": {
        "72": {595, 842},
        "96": {794, 1123},
        "150": {1240, 1754},
        "300": {2480, 3508}
    }
}

Now, Only when I try to fetch A4 at 72 ppi, i get the dimension in reversed order.

>>> print(paperSizes["A4"]["72"])
{842, 595}
>>> print(paperSizes["A4"]["96"])
{794, 1123}

This has been causing issues for me and I simply cant understand why this is happening Is there an underlying dictionary behavior I am unaware of?

Upvotes: 1

Views: 37

Answers (2)

kederrac
kederrac

Reputation: 17322

set objects will not guarantee the order, you could use lists, to convert your sets to lists you can use:

paperSizes = {k: {i: sorted(j) for i, j in d.items()} for k, d in paperSizes.items()}

now you can print and get the exptected result:

print(paperSizes["A4"]["72"])

# [595, 842]

Upvotes: 0

Shehan
Shehan

Reputation: 186

Rather than taking a set use lists and it will solve your problem.

Upvotes: 5

Related Questions