Python 如何更新列表字典中的值

Python 如何更新列表字典中的值,python,dictionary,Python,Dictionary,我正在尝试更新列表字典中的值 例:我有一个字典输入: d={0: [0,1], 1:[1],2:[2],3:[3]} 和配对[0,2] 我想用0,2替换dict中的每一个0,并将每一个大于2的值增加1,所以这里是预期的输出: {0: [0,2,1], 1:[1],2:[2],3:[4]} for i, nested in enumerate(l): # replace all 0 values with 1, 2 while 0 in nested: zero

我正在尝试更新列表字典中的值 例:我有一个字典输入:

d={0: [0,1], 1:[1],2:[2],3:[3]}
和配对
[0,2]
我想用0,2替换dict中的每一个0,并将每一个大于2的值增加1,所以这里是预期的输出:

{0: [0,2,1], 1:[1],2:[2],3:[4]}
for i, nested in enumerate(l):
    # replace all 0 values with 1, 2
    while 0 in nested:
        zero_index = nested.index(0)
        nested[zero_index:zero_index + 1] = [1, 2]
    # increment values in the nested list if the index is over 2:
    if i > 2:
        nested[:] = [v + 1 for v in nested]
我试着迭代所有的值,但没有成功

def update_dict(_dict,_pair):
    for i in range(len(_dict.keys())):
        for j in range(len(_dict.values()[i])):
            if dict[i][j]==_pair[0]:
                dict[i][j].remove(_pair[0])
                dict[i].append(_pair)
    return _dict
我如何做到这一点?
提前感谢您的帮助

这里您不需要词典,您需要的是列表;您有一系列从0开始的有序键,列表索引将更有效地满足这一需求:

l = [[0,1], [1], [2], [3]]
您可以生成所需的输出:

{0: [0,2,1], 1:[1],2:[2],3:[4]}
for i, nested in enumerate(l):
    # replace all 0 values with 1, 2
    while 0 in nested:
        zero_index = nested.index(0)
        nested[zero_index:zero_index + 1] = [1, 2]
    # increment values in the nested list if the index is over 2:
    if i > 2:
        nested[:] = [v + 1 for v in nested]

这会改变原来的嵌套列表。

这里不需要字典,您需要列表;您有一系列从0开始的有序键,列表索引将更有效地满足这一需求:

l = [[0,1], [1], [2], [3]]
您可以生成所需的输出:

{0: [0,2,1], 1:[1],2:[2],3:[4]}
for i, nested in enumerate(l):
    # replace all 0 values with 1, 2
    while 0 in nested:
        zero_index = nested.index(0)
        nested[zero_index:zero_index + 1] = [1, 2]
    # increment values in the nested list if the index is over 2:
    if i > 2:
        nested[:] = [v + 1 for v in nested]

这将改变原来的嵌套列表。

如果您仍然想使用字典,这将执行以下操作:

d = {0: [0,1], 1:[1],2:[2],3:[3]}

for k, li in d.items():
    for i, v in enumerate(li[:]):
        if v == 0:
            li.insert(i + 1, 2)
        elif v > 2:
            d[k][i] += 1

print(d)
# {0: [0, 2, 1], 1: [1], 2: [2], 3: [4]}

请记住,不能保证键的顺序。

如果您仍然想使用字典,可以这样做:

d = {0: [0,1], 1:[1],2:[2],3:[3]}

for k, li in d.items():
    for i, v in enumerate(li[:]):
        if v == 0:
            li.insert(i + 1, 2)
        elif v > 2:
            d[k][i] += 1

print(d)
# {0: [0, 2, 1], 1: [1], 2: [2], 3: [4]}

请记住,不能保证钥匙的顺序。

有很多解决方案。例如:

d = {0: [0,1], 1:[1],2:[2],3:[3]}
new_d = dict()
for k in d:
    new_d[k] = sum([i == 0 and [0, 2] or i > 2 and \
                    [i + 1] or [i] for i in d[k]], [])


有大量的解决方案。例如:

d = {0: [0,1], 1:[1],2:[2],3:[3]}
new_d = dict()
for k in d:
    new_d[k] = sum([i == 0 and [0, 2] or i > 2 and \
                    [i + 1] or [i] for i in d[k]], [])


另一种替代解决方案:

d={0: [0,1], 1:[1],2:[2],3:[3]}

for k,a in d.items():
    for key, digit in enumerate(a):
        if digit == 0: a[key:key+1] = [0,2]
        if digit > 2: a[key] += 1

print(d)
输出:

{0: [0, 2, 1], 1: [1], 2: [2], 3: [4]}

另一种替代解决方案:

d={0: [0,1], 1:[1],2:[2],3:[3]}

for k,a in d.items():
    for key, digit in enumerate(a):
        if digit == 0: a[key:key+1] = [0,2]
        if digit > 2: a[key] += 1

print(d)
输出:

{0: [0, 2, 1], 1: [1], 2: [2], 3: [4]}
“如何更新字典中列表中的值?”是一个好问题。假设我有一个列表字典,我想更改其中一个列表中的值

导入副本
i=1
输入句子dict={i:[]}
输入句子[1]=“[‘我’、‘有’、‘a’、‘礼物’、‘送给’、‘胭脂红’、‘.””
输入句子[2]=“我”、“给”、“它”、“给”、“她”、“她”
导入pprint
pp=pprint.预印机(缩进=4)
打印('input_句子_dict:')
pp.pprint(输入句子)
'''
输入句子内容:
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给”、“它”、“给”、“她”、“她”]]
'''
输出句子dict=copy.deepcopy(输入句子dict)
打印('输出\语句\命令:')
pp.pprint(输出句子)
'''
输出句子内容:
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给”、“它”、“给”、“她”、“她”]]
'''
#索引示例:
打印(输出句子记录[2][1:3])
#['give','it']
通过索引,您可以轻松地更改其中一个列表中的值。例如:

d = {0: [0,1], 1:[1],2:[2],3:[3]}
new_d = dict()
for k in d:
    new_d[k] = sum([i == 0 and [0, 2] or i > 2 and \
                    [i + 1] or [i] for i in d[k]], [])
output\u句子\u dict[2][2]=“一本书”
pp.pprint(输出句子)
'''
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给了”、“一本书”、“给了”、“她”、“她”]]
'''

注意。如果您只是复制一本词典(例如,
dict2=dict1
),对其中一本词典的编辑会影响另一本词典。您需要“深度复制”源字典,以确保处理真正不同的对象,使源对象不受干扰

导入副本
dict2=复制.深度复制(dict1)
然后,您可以编辑
dict2
,保持
dict1
不变

这在StackOverflow线程中有很好的描述:

使用“浅拷贝”,例如

将导致无声错误。在上面的示例中,如果不使用
copy.deepcopy(dict)
,对
output\u-statement\u-dict
所做的更改也会自动传播到
input\u-statement\u-dict

“如何更新字典中列表中的值?”是一个好问题。假设我有一个列表字典,我想更改其中一个列表中的值

导入副本
i=1
输入句子dict={i:[]}
输入句子[1]=“[‘我’、‘有’、‘a’、‘礼物’、‘送给’、‘胭脂红’、‘.””
输入句子[2]=“我”、“给”、“它”、“给”、“她”、“她”
导入pprint
pp=pprint.预印机(缩进=4)
打印('input_句子_dict:')
pp.pprint(输入句子)
'''
输入句子内容:
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给”、“它”、“给”、“她”、“她”]]
'''
输出句子dict=copy.deepcopy(输入句子dict)
打印('输出\语句\命令:')
pp.pprint(输出句子)
'''
输出句子内容:
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给”、“它”、“给”、“她”、“她”]]
'''
#索引示例:
打印(输出句子记录[2][1:3])
#['give','it']
通过索引,您可以轻松地更改其中一个列表中的值。例如:

d = {0: [0,1], 1:[1],2:[2],3:[3]}
new_d = dict()
for k in d:
    new_d[k] = sum([i == 0 and [0, 2] or i > 2 and \
                    [i + 1] or [i] for i in d[k]], [])
output\u句子\u dict[2][2]=“一本书”
pp.pprint(输出句子)
'''
{1:['I'、'have'、'a'、'gift'、'for'、'Carmine'、',
2:[“我”、“给了”、“一本书”、“给了”、“她”、“她”]]
'''

注意。如果您只是复制一本词典(例如,
dict2=dict1
),对其中一本词典的编辑会影响另一本词典。您需要“深度复制”源字典,以确保处理真正不同的对象,使源对象不受干扰

导入副本
dict2=复制.深度复制(dict1)
然后,您可以编辑
dict2
,保持
dict1
不变

这在StackOverflow线程中有很好的描述:

使用“浅拷贝”,例如


将导致无声错误。在上面的示例中,如果您不使用
copy.deepcopy(dict)
,对
output\u-statement\u-dict
所做的更改也会自动传播到
input\u-statement\u-dict

请提供预期的输出为什么您甚至有字典?为什么不列一份清单呢?输入
[[0,1],[1],[2],[3]
,输出
[[0,2,1],[1],[2],[4]]
。列表是否总是按顺序排列?0是否总是要用一对替换的值?请提供预期的输出为什么您甚至有字典?为什么不列一份清单呢?输入
[[0,1],[1],[2],[3]
,输出
[[0,2,1],[1],[2],[4]]
。列表是否总是按顺序排列?0是否总是要用一对替换的值?