删除每个键中每个键的值

时间:2013-11-30 20:44:07

标签: python dictionary

sample_dict = {'i.year': ['1997', '1997'], 'i.month': ['March', 'April'], 'j.month': ['March', 'April'], 'j.year': ['1997', '2003']}

我们如何比较i.year和j.year中的每个元素,如果元素彼此相等,则删除该值中该元素的元素 在每个键处,然后转到下一个元素并继续该过程。每个值的长度将始终相同。 不使用导入

所以基本上我想说的是:

为了这个问题,只比较i.year和j.year中的元素:

-> {'i.year': ['1997', '1997'], 'i.month': ['March', 'April'], 'j.month': ['March', 'April'], 'j.year': ['1997', '2003']}
-> The first element in i.year is equal to the first element in j.year, so delete the first element in each value in every key.

我们得到:

-> {'i.year': ['1997'], 'i.month': ['April'], 'j.month': ['April'], 'j.year': ['2003']}
-> The element now in i.year and j.year is the same so we're done. Return that dictionary

另一个例子:

-> {'i.year': ['1997', '1997', '2009'], 'i.month': ['March', 'April', 'June'], 'j.month': ['March', 'April', 'June'], 'j.year': ['1997', '2003', '2010']}
-> The first element in i.year is equal to the first element in j.year, so delete the first element in each value in every key.

我们得到:

-> {'i.year': ['1997', '2009'], 'i.month': ['April', 'June'], 'j.month': ['April', 'June'], 'j.year': ['2003', '2010']}
-> Now the first element in i.year and j.year are different so we move to the next element which is '2009' and '2010'
-> '2009' and '2010' are different so we move to the next element, since there's none we are done. Return that dictionary.

最后一个例子:

-> {'i.year': ['1996', '1997', '2010'], 'i.month': ['March', 'April', 'June'], 'j.month': ['March', 'April', 'June'], 'j.year': ['1997', '2003', '2010']}
-> The first element in i.year is not equal to the first element in j.year, so we move on to the next element.
-> '1997' does not equal to '2003' so we move on to the next element
-> '2010' is equal to '2010' so we delete each element in every key at the index

我们得到:

-> {'i.year': ['1996', '1997'], 'i.month': ['March', 'April'], 'j.month': ['March', 'April'], 'j.year': ['1997', '2003']}
-> There are no more elements to move on to, so we are done, we return this dictionary.

我有这个想法,但不能把它变成python代码:

对于i.month中的每个元素循环遍历j.year中的每个元素,并且如果元素不相等则遍历整个字典并删除元素 在那个指数。

1 个答案:

答案 0 :(得分:0)

您可以先找到i.yearj.year中元素不同的索引,然后迭代dict并过滤掉那些不在这些索引中的项目:

def solve(d, *keys):
    indexes = [i for i, x in enumerate(zip(*(d[k] for k in keys)))
                                                                if len(set(x)) != 1]
    return {k:[v[x] for x in indexes] for k, v in d.items()}

<强>演示:

>>> d = {'i.year': ['1997', '1997'], 'i.month': ['March', 'April'], 'j.month': ['March', 'April'], 'j.year': ['1997', '2003']}
>>> solve(d, 'i.year', 'j.year')
{'i.year': ['1997'], 'j.month': ['April'], 'i.month': ['April'], 'j.year': ['2003']}

>>> d = {'i.year': ['1997', '1997', '2009'], 'i.month': ['March', 'April', 'June'], 'j.month': ['March', 'April', 'June'], 'j.year': ['1997', '2003', '2010']}
>>> solve(d, 'i.year', 'j.year')
{'i.year': ['1997', '2009'], 'j.month': ['April', 'June'], 'i.month': ['April', 'June'], 'j.year': ['2003', '2010']}

>>> d = {'i.year': ['1996', '1997', '2010'], 'i.month': ['March', 'April', 'June'], 'j.month': ['March', 'April', 'June'], 'j.year': ['1997', '2003', '2010']}
>>> solve(d, 'i.year', 'j.year')
{'i.year': ['1996', '1997'], 'j.month': ['March', 'April'], 'i.month': ['March', 'April'], 'j.year': ['1997', '2003']}