将 Python 字典提取到列表时的值顺序

发布于 2025-01-04 21:09:16 字数 656 浏览 4 评论 0原文

我试图通过这样做从字典列表中获取所有值(除了对应于特定键的值):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2025-01-11 21:09:16

如果您知道您的字典可能包含的可能键的列表,则以下解决方案可能适合您:

allkeys = ...  # might be known; or obtained from available dicts by union;
               # 'xKey' can be removed at this stage to simplify the list
               # comprehension that follows
sortedKeys = sorted(allKeys)
list_of_values = [[d.get(k) for k in sortedKeys if k in d]
                                                   for d in list_of_dicts] 

不过它可能比 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:

allkeys = ...  # might be known; or obtained from available dicts by union;
               # 'xKey' can be removed at this stage to simplify the list
               # comprehension that follows
sortedKeys = sorted(allKeys)
list_of_values = [[d.get(k) for k in sortedKeys if k in d]
                                                   for d in list_of_dicts] 

It might be slower than iteritems though. The if k in d part can be removed if all the dicts contain the same set of keys.

飘逸的'云 2025-01-11 21:09:16

平常的练习是这样的。

  1. 将你的字典构建为字典。速度非常快。

  2. 在极少数情况下,当您需要密钥才能执行以下两项操作之一时:

    • 将整个内容转换为 OrderedDict

    • 对键进行排序。 对于排序中的 k( some_dict.keys() ):

    选择是排序成本的摊销之一。如果你正在做一件事,那就排序。如果您正在做几件事,请构建一个 OrderedDict。

  3. 在非常非常罕见的情况下,值必须按某种顺序排列,请执行此操作。

    ordered_values = list(sorted(some_dictionary.values()))
    

The usual drill is this.

  1. Build your dictionary as a dict. Speed is excellent.

  2. 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.

  3. In the really, really rare case where the values must be in some order, do this.

    ordered_values = list( sorted( some_dictionary.values() ) )
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文