矩阵作为字典;是安全吗? [英] Matrix as a dictionary; is it safe?
问题描述
我知道密钥的顺序不能保证,这样做还可以,但究竟是什么意思,值的顺序是不能保证的?
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
我的矩阵列是否正确构造?假设我有3行,在这个 signatures_dict [i] =行
行,行
将始终有1,2 ,3,4,5.什么将 signatures_dict
be?
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
或类似
1 2 3 4 5
1 4 3 2 5
5 1 3 4 2
?我担心跨平台支持。
在我的应用程序中,行是单词和列文档,所以我可以说第一列是第一个文件?
In my application, the rows are words and the columns documents, so can I say that the first column is the first document?
* python字典中的键()和值()的顺序是否保证相同? / p>
*Are order of keys() and values() in python dictionary guaranteed to be the same?
推荐答案
您将保证每一行都有 1 2 3 4 5
。它不会重新排序它们。缺少 values()
的排序是指如果您调用 signatures_dict.values()
,则值可以以任何顺序出来。但值是行,而不是每行的元素。每一行都是一个列表,列表维护它们的顺序。
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.
如果你想要一个维护订单的dict,Python也有这样一个: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you want a dict which maintains order, Python has that too: https://docs.python.org/2/library/collections.html#collections.OrderedDict
这篇关于矩阵作为字典;是安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!