-1

I know that the order of the keys is not guaranteed and that's OK, but what exactly does it mean that the order of the values is not guaranteed as well*?

For example, I am representing a matrix as a dictionary, like this:

signatures_dict = {}
M = 3
for i in range(1, M):
    row = []
    for j in range(1, 5):
        row.append(j)
    signatures_dict[i] = row
print signatures_dict

Are the columns of my matrix correctly constructed? Let's say I have 3 rows and at this signatures_dict[i] = row line, row will always have 1, 2, 3, 4, 5. What will signatures_dict be?

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

or something like

1 2 3 4 5
1 4 3 2 5
5 1 3 4 2

? I am worried about cross-platform support.

In my application, the rows are words and the columns documents, so can I say that the first column is the first document?

*Are order of keys() and values() in python dictionary guaranteed to be the same?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305

2 Answers2

1

You will guaranteed have 1 2 3 4 5 in each row. It will not reorder them. The lack of ordering of values() refers to the fact that if you call signatures_dict.values() the values could come out in any order. But the values are the rows, not the elements of each row. Each row is a list, and lists maintain their order.

If you want a dict which maintains order, Python has that too: https://docs.python.org/2/library/collections.html#collections.OrderedDict

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • John thanks for the upvote. In my application, the rows are words and the columns documents, so can I say that the first column is the first document? – gsamaras May 07 '16 at 15:28
  • Oh great John, so the order of the keys will be the same of the order of the values. With that I mean that `signatures_dict.keys()[0]` will fetch the first key (whichever is stored first) and likewise for `signatures_dict.values()[0]`, which will fetch the corresponding value for the first key. – gsamaras May 07 '16 at 15:38
1

Why not use a list of lists as your matrix? It would have whatever order you gave it;

In [1]: matrix = [[i for i in range(4)] for _ in range(4)]

In [2]: matrix
Out[2]: [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]

In [3]: matrix[0][0]
Out[3]: 0

In [4]: matrix[3][2]
Out[4]: 2
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Ronald, I thought I should mention that, I deserved your downvote for didn't. Because I was told so: http://stackoverflow.com/questions/37062078/storing-the-result-of-minhash You deserve an upvote for dropping some code though. :) – gsamaras May 07 '16 at 15:45
  • It depends on what is more important in your application. If the ability to always iterate over the data in the same order is important, I would suggest a list of lists. If the first index is not a number, then you'd better go with a dictionary. BTW, there is no Ronald here :-) – Roland Smith May 07 '16 at 15:46
  • @gsamaras: but your keys do not contain anything meaningful beyond what a list index gives you. If you have a *sparse* matrix (where rows are omitted), then a dictionary may make sense as it can be more space efficient that way. – Martijn Pieters May 08 '16 at 14:22