将 Python 字典提取到列表时的值顺序
我试图通过这样做从字典列表中获取所有值(除了对应于特定键的值):
fv = [[v for (k,v) in d.iteritems() if k is not 'xKey'] for d in someDict]
其中 someDict
就像:
[{xKey:0.1,yKey:0.2,zKey:0.3},{yKey:0.9,xKey:0.7,zKey:0.4}...]
我知道字典没有固有的顺序。但我的列表 fv 需要按顺序排列值。我正在考虑对按键上的字典进行排序,然后执行我刚才所做的操作。但这保证有效吗?
我知道使用 OrderedDict 是一种选择,但与 dict 相比,它的性能似乎较差,这对我来说是一个问题,因为我的字典通常会包含大量数据。
更新:当我说我需要按顺序排列值时,它们实际上并不需要排序。我的意思是我需要能够每次以固定的确定性顺序检索值列表。在上面的示例中,我总是想得到 [[0.2,0.3],[0.9,0.4]] 尽管它本身可能不是排序顺序。排序将强制执行一个确定性顺序。我真正关心的是维护最终列表中值的位置。例如 yKey 的值必须始终是每个列表中的第一个值,zKey 的值必须始终是每个列表中的第二个值依此类推,即使 ykey、zkey 等在字典中可以按任何顺序排列。
I am trying to get all values (except corresponding to a particular key) from a list of dicts by doing this:
fv = [[v for (k,v) in d.iteritems() if k is not 'xKey'] for d in someDict]
where someDict
is like:
[{xKey:0.1,yKey:0.2,zKey:0.3},{yKey:0.9,xKey:0.7,zKey:0.4}...]
I know a dict doesn't have an inherent order. But my list of lists fv needs to have the values in order. I am thinking about sorting the dict on key and then doing what I just did. But is that guaranteed to work?
I know using OrderedDict
is an option but it also seems to have inferior performance as compared to dict, which would be a concern for me since my dictionary is typically going to have huge amount of data.
Update : When I say I need values in order, they don't really have to be sorted.What I mean is I need to be able to retrieve the list of values in a fixed deterministic order each time.In the above example, I always want to get [[0.2,0.3],[0.9,0.4]] although it may not be a sorted order per se. Sorting would enforce one deterministic order.What I really care about is maintaining the position of values in the final list.e.g. Value of yKey must always be the first value in each list, the value of zKey must always be the second value in each list and so on even though ykey, zkey etc may be in any order in the dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您知道您的字典可能包含的可能键的列表,则以下解决方案可能适合您:
不过它可能比
iteritems
慢。如果所有字典都包含相同的键集,则可以删除if k in d
部分。If you know the list of possible keys that your dicts may contain, the following solution might work for you:
It might be slower than
iteritems
though. Theif k in d
part can be removed if all the dicts contain the same set of keys.平常的练习是这样的。
将你的字典构建为字典。速度非常快。
在极少数情况下,当您需要密钥才能执行以下两项操作之一时:
将整个内容转换为 OrderedDict
对键进行排序。
对于排序中的 k( some_dict.keys() ):
选择是排序成本的摊销之一。如果你正在做一件事,那就排序。如果您正在做几件事,请构建一个 OrderedDict。
在非常非常罕见的情况下,值必须按某种顺序排列,请执行此操作。
The usual drill is this.
Build your dictionary as a dict. Speed is excellent.
In the rare cases when you need the keys in order do one of the two:
Convert the entire thing to an OrderedDict
Sort the keys.
for k in sorted( some_dict.keys() ):
The choice is one of amortization of the sort cost. If you're doing one thing, sort. if you're doing several things, build an OrderedDict.
In the really, really rare case where the values must be in some order, do this.