Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在每个键的值处删除每个元素_Python_Dictionary - Fatal编程技术网

Python 在每个键的值处删除每个元素

Python 在每个键的值处删除每个元素,python,dictionary,Python,Dictionary,我们如何比较i.year和j.year中的每个元素,如果元素彼此相等,则删除值中该索引处的元素 在每个关键点和,然后移动到下一个元素并继续该过程。每个值的长度将始终相同。不使用进口商品 所以基本上我想说的是: 为了解决这个问题,只比较i.year和j.year中的元素: sample_dict = {'i.year': ['1997', '1997'], 'i.month': ['March', 'April'], 'j.month': ['March', 'April'], 'j.year':

我们如何比较i.year和j.year中的每个元素,如果元素彼此相等,则删除值中该索引处的元素 在每个关键点和,然后移动到下一个元素并继续该过程。每个值的长度将始终相同。不使用进口商品

所以基本上我想说的是:

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

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

-> {'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', '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': ['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': ['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'], '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', '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': ['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': ['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': ['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', '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': ['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
我有这个想法,但无法将其转化为python代码:

对于i.month中的每个元素,通过j.year中的每个元素循环,如果元素不相等,则通过整个字典循环并删除该元素
在该索引处。

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

-> {'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.
演示:

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()}

谢谢,Ashwini,但是有没有办法不用zip就可以做到这一点?@Sc4r是的,你可以,然后你必须写你的,这在这里是不必要的。哦,谢谢,我应该在你的代码中修改哪一行而不是我们拥有的那一行更大。