Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 - Fatal编程技术网

Python 从字典中删除嵌套值列表中的特定值

Python 从字典中删除嵌套值列表中的特定值,python,Python,目前,我有字典: d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]} d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': ['w3', ['w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]} 格式化后,如下所示(为了便于阅读): 及 to_match是一

目前,我有字典:

d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}
d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': ['w3', ['w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}
格式化后,如下所示(为了便于阅读):

to_match是一个字符串,如果它与dictionaryd中的一个键匹配,它将用该列表中的第一项替换None,然后从列表中删除它自己。我对如何进行这项工作感到非常困惑

例如…

因此,由于要匹配的'm2',它将查找键m2,用列表中的第一项替换None,并将自己从列表中删除。看起来像这样:

之前:

'm2': [None, ['w3', 'w1', 'w2']]
在以下情况下,“m2”应该是什么样子:

'm2': ['w3', ['w1', 'w2']]
整本字典:

d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}
d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': ['w3', ['w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}
我怎样才能把整本字典改成这样

当前代码:

d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}

to_match = 'm2'

def replace(d: dictionary, to_match):
   for key, value in d.items():
       if to_match in key:



   return 



replace(d, to_match)

对错误的抵御能力不强,但:

d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}

to_match = 'm2'

def replace(d, to_match):
    value_list = d.get(to_match)
    if value_list is not None:
        # assuming your values are always lists of 2
        head, remainder = value_list
        if remainder:
            new_value_list = [remainder[0], remainder[1:]]
        else:
            # just guessing? really depends on what your requirements are...
            new_value_list = [None, []]
        d[to_match] = new_value_list
        return head


replace(d, to_match)
print(d)

对错误的抵御能力不强,但:

d = {'m1': [None, ['w3', 'w2', 'w1']], 'm2': [None, ['w3', 'w1', 'w2']], 'm3': [None, ['w2', 'w1', 'w3']]}

to_match = 'm2'

def replace(d, to_match):
    value_list = d.get(to_match)
    if value_list is not None:
        # assuming your values are always lists of 2
        head, remainder = value_list
        if remainder:
            new_value_list = [remainder[0], remainder[1:]]
        else:
            # just guessing? really depends on what your requirements are...
            new_value_list = [None, []]
        d[to_match] = new_value_list
        return head


replace(d, to_match)
print(d)

下面这段代码将帮助您解决问题。 您不需要在键上运行for循环,因为python字典就像哈希表一样,对键进行哈希处理以匹配值

def replace(d, to_match):
    if to_match in d:
        d[to_match][0] = d[to_match][1].pop(0)
    return

我不知道您的其他要求,但这应该可以满足您的需要

以下代码将帮助您解决问题。 您不需要在键上运行for循环,因为python字典就像哈希表一样,对键进行哈希处理以匹配值

def replace(d, to_match):
    if to_match in d:
        d[to_match][0] = d[to_match][1].pop(0)
    return

我不知道您的其他要求,但这应该满足您的需要

您将一个值存储为一个包含两个元素的列表,其中第二个元素本身就是一个列表,这有什么意义?这使得您的数据结构更难使用…您也不需要在字典中的项目上迭代来查找
'm2'
…您只需执行
d.get('m2')
,如果它是非
None
,则执行必要的替换。当您说
to_match
应该“match”时,您的确切意思是什么钥匙?平等?@Hamms是的,所以如果'm2'='m2'。塔纳贝哦,我不知道,谢谢。至于意义,不幸的是,这正是我要处理的字典的结构……你将一个值存储为一个包含两个元素的列表,其中第二个元素本身就是一个列表,这有什么意义吗?这使得您的数据结构更难使用…您也不需要在字典中的项目上迭代来查找
'm2'
…您只需执行
d.get('m2')
,如果它是非
None
,则执行必要的替换。当您说
to_match
应该“match”时,您的确切意思是什么钥匙?平等?@Hamms是的,所以如果'm2'='m2'。塔纳贝哦,我不知道,谢谢。至于意义,不幸的是,这只是我必须处理的字典的结构…非常干净的解决方案。假设数据结构没有偏差,但非常适合现有内容。非常干净的解决方案。假设数据结构中没有偏差,但非常适合所存在的内容。