简体   繁体   English

在带有列表列表的字典上操作

[英]Operating on a dictionary with list of lists

I'm trying to update a list of lists within a dictionary where I would like to remove any list (within the list of lists) that has an item that is smaller than whatever I'm comparing it with. 我正在尝试更新字典中的列表列表,在该列表中我想删除任何列表项(列表列表中)的项目小于我与之比较的列表。 For example, given the dict of list of lists below.. 例如,给定下面列表的字典。

mydict = {3603172: [
    ['M2-HCg18', 12.00, 12.00, 9.00, '12 x 12 x 9'],
    ['M2-HCg18', 16.00, 14.00, 8.00, '16 x 14 x 8'],
    ['M2-HCg18', 16.00, 14.00, 14.00, '16 x 14 x 14'],
    ['M2-HCg18', 18.00, 18.00, 18.00, '18 x 18 x 18'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ['M2-HCg18', 7.25, 4.75, 1.00, 'Bubble Mailer #4 7.25 x 5'],
    ['M2-HCg18', 9.25, 7.25, 1.00, 'Bubble Mailer DVD 10.25 x 7.25'],
    ['M2-HCg18', 28.00, 14.00, 8.00, '28x14x8'],
    ['M2-HCg18', 28.00, 14.00, 0.25, '28x14x4'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ]}

The above dict has a key (3603172) whose value is a list of lists. 上面的dict有一个键(3603172),其值是列表列表。 Note that there may be duplicate lists within the list of lists. 请注意,列表列表中可能有重复的列表。 What I'd like to do is compare the 4th item in any one of those inner lists with a given value and remove the list if the 3rd item in the list is smaller. 我想做的是比较任何内部列表中的第四个项目与给定的值,如果列表中的第三个项目较小,则删除该列表。 So if my value is 0.5, I'd like to remove all the inner lists that are less than 0.5 at index 3. 因此,如果我的值为0.5,我想删除索引3上所有小于0.5的内部列表。

I know that I can use mydict.itervalues() and operate on the value which is the list of lists and then run a compare on index 3 using an if statement with 0.5 but I don't really know a clean way of removing the list if the value at index 3 is smaller than 0.5. 我知道我可以使用mydict.itervalues()并操作作为列表列表的值,然后使用带有0.5的if语句对索引3进行比较,但是我真的不知道删除列表的干净方法如果索引3的值小于0.5。 In the example above, I'd like to remove all lists with 0.25 at index 3 (smaller than 0.5). 在上面的示例中,我想删除索引为3(小于0.5)的0.25的所有列表。 What would be a good way to do this? 什么是做到这一点的好方法?

The concept of python list filtering is demonstrated in the following post: List filtering: list comprehension vs. lambda + filter python列表过滤的概念在以下文章中得到了说明: 列表过滤:列表理解与lambda +过滤器

That is, you can iterate over the keys, and for each key, set its new value to be its old value filtered using one of the offered methods. 也就是说,您可以遍历键,并且对于每个键,使用提供的方法之一将其新值设置为已过滤的旧值。

Use dict comprehension. 使用dict理解。

>>> mydict = {3603172: [
    ['M2-HCg18', 12.00, 12.00, 9.00, '12 x 12 x 9'],
    ['M2-HCg18', 16.00, 14.00, 8.00, '16 x 14 x 8'],
    ['M2-HCg18', 16.00, 14.00, 14.00, '16 x 14 x 14'],
    ['M2-HCg18', 18.00, 18.00, 18.00, '18 x 18 x 18'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ['M2-HCg18', 7.25, 4.75, 1.00, 'Bubble Mailer #4 7.25 x 5'],
    ['M2-HCg18', 9.25, 7.25, 1.00, 'Bubble Mailer DVD 10.25 x 7.25'],
    ['M2-HCg18', 28.00, 14.00, 8.00, '28x14x8'],
    ['M2-HCg18', 28.00, 14.00, 0.25, '28x14x4'],
    ['M2-HCg18', 16.00, 10.25, 0.25, 'Bubble Mailer #5 10.5 x 16'],
    ]}
>>> {i:[x for x in j if x[3] > 0.5] for i,j in mydict.items()}
{3603172: [['M2-HCg18', 12.0, 12.0, 9.0, '12 x 12 x 9'], ['M2-HCg18', 16.0, 14.0, 8.0, '16 x 14 x 8'], ['M2-HCg18', 16.0, 14.0, 14.0, '16 x 14 x 14'], ['M2-HCg18', 18.0, 18.0, 18.0, '18 x 18 x 18'], ['M2-HCg18', 7.25, 4.75, 1.0, 'Bubble Mailer #4 7.25 x 5'], ['M2-HCg18', 9.25, 7.25, 1.0, 'Bubble Mailer DVD 10.25 x 7.25'], ['M2-HCg18', 28.0, 14.0, 8.0, '28x14x8']]}
>>> pprint({i:[x for x in j if x[3] > 0.5] for i,j in mydict.items()})
{3603172: [['M2-HCg18', 12.0, 12.0, 9.0, '12 x 12 x 9'],
           ['M2-HCg18', 16.0, 14.0, 8.0, '16 x 14 x 8'],
           ['M2-HCg18', 16.0, 14.0, 14.0, '16 x 14 x 14'],
           ['M2-HCg18', 18.0, 18.0, 18.0, '18 x 18 x 18'],
           ['M2-HCg18', 7.25, 4.75, 1.0, 'Bubble Mailer #4 7.25 x 5'],
           ['M2-HCg18', 9.25, 7.25, 1.0, 'Bubble Mailer DVD 10.25 x 7.25'],
           ['M2-HCg18', 28.0, 14.0, 8.0, '28x14x8']]}

Iterate over the list of lists with reversed and remove the lists from the original dicts 遍历具有反向的列表列表,然后从原始字典中删除列表

k = mydict.keys()[0]
for  val in reversed(mydict[k]):
    if val[3]  < .5:
        mydict[k].remove(val)
print(mydict)

If you have more than one key: 如果您有多个键:

for  v in mydict.itervalues():
    for val in reversed(v):
        if val[3] < .5:
            v.remove(val)
print(mydict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM