删除循环内的特定字典值
Removing specific dictionary values inside a loop
我正在尝试制作一个上下文无关的语法简化软件。
从字典的值甚至键值对中删除某些特定项目时,我被卡住了。
问题是它不遵循某种模式。
如果元素属于V1,我需要将它保存在字典中。
(V1 是派生终端的所有值的列表,这些人是我唯一需要保留在我的字典中的人,但它并不那么简单)
如果元素不属于V1且字典的值是字符串,我需要删除元素。
如果元素不属于V1并且字典的值是一个列表,我需要检查它是否是该列表的单个元素,如果是,删除Value。
失败的循环在此处。
把修改字典搞不懂逻辑的地方打印出来了
counter = 0
for k,v in derivations.items():
derivationsCount = len(v)
while counter < derivationsCount:
if lista_ou_string(v[counter]): # returns True for lists, False for else
sizeOfList = len(v[counter])
counter2 = 0
while counter2 <= (sizeOfList - 1):
if v[counter][counter2] not in V1:
if derivationsCount == 1:
print("# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()")
else:
print("# NEED TO DELETE ONLY THE VALUE FROM derivations.items()")
counter2 += 1
else: # strings \/
if v[counter] not in V1:
if derivationsCount == 1:
print("# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()")
else:
print("# NEED TO DELETE ONLY THE VALUE FROM derivations.items()")
else:
print("# DO NOT DELETE ANYTHING! ALL LISTS ELEMENTS BELONGS TO 'V1'")
counter += 1
如果你想从字典中删除键值对,使用del
:
>>> my_dictionary = {'foo':'bar', 'boo':'baz'}
>>> del my_dictionary['foo']
>>> my_dictionary
{'boo': 'baz'}
如果你想删除值,但保留键,你可以尝试分配键None
:
>>> my_dictionary = {'foo':'bar', 'boo':'baz'}
>>> my_dictionary['foo'] = None
>>> my_dictionary
{'foo': None, 'boo': 'baz'}
人们不想在遍历字典(或列表)时修改它。因此我创建了 derivations
- new_derivations
的副本并修改了 new_derivations
:
import copy
new_derivations = copy.deepcopy(derivations)
for k, v in derivations.items():
for vi in v:
if (lista_ou_string(vi) and not set(vi).issubset(V1)) or vi not in V1:
if len(v) == 1:
# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()
del new_derivations[k]
break
else:
# NEED TO DELETE ONLY THE VALUE FROM derivations.items()
idx = new_derivations[k].index(vi)
del new_derivations[k][idx]
我实际上会以不同的方式实现上述代码:与其考虑从 derivations
中删除项目,不如考虑何时应将元素添加到列表中。那么代码就变得简单多了:
new_derivations = {}
for k, v in derivations.items():
nv = [vi for vi in v if ((isinstance(vi, list) and set(vi).issubset(V1))
or vi in V1)]
if nv:
new_derivations[k] = nv
我正在尝试制作一个上下文无关的语法简化软件。 从字典的值甚至键值对中删除某些特定项目时,我被卡住了。
问题是它不遵循某种模式。
如果元素属于V1,我需要将它保存在字典中。 (V1 是派生终端的所有值的列表,这些人是我唯一需要保留在我的字典中的人,但它并不那么简单)
如果元素不属于V1且字典的值是字符串,我需要删除元素。
如果元素不属于V1并且字典的值是一个列表,我需要检查它是否是该列表的单个元素,如果是,删除Value。
失败的循环在此处。 把修改字典搞不懂逻辑的地方打印出来了
counter = 0
for k,v in derivations.items():
derivationsCount = len(v)
while counter < derivationsCount:
if lista_ou_string(v[counter]): # returns True for lists, False for else
sizeOfList = len(v[counter])
counter2 = 0
while counter2 <= (sizeOfList - 1):
if v[counter][counter2] not in V1:
if derivationsCount == 1:
print("# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()")
else:
print("# NEED TO DELETE ONLY THE VALUE FROM derivations.items()")
counter2 += 1
else: # strings \/
if v[counter] not in V1:
if derivationsCount == 1:
print("# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()")
else:
print("# NEED TO DELETE ONLY THE VALUE FROM derivations.items()")
else:
print("# DO NOT DELETE ANYTHING! ALL LISTS ELEMENTS BELONGS TO 'V1'")
counter += 1
如果你想从字典中删除键值对,使用del
:
>>> my_dictionary = {'foo':'bar', 'boo':'baz'}
>>> del my_dictionary['foo']
>>> my_dictionary
{'boo': 'baz'}
如果你想删除值,但保留键,你可以尝试分配键None
:
>>> my_dictionary = {'foo':'bar', 'boo':'baz'}
>>> my_dictionary['foo'] = None
>>> my_dictionary
{'foo': None, 'boo': 'baz'}
人们不想在遍历字典(或列表)时修改它。因此我创建了 derivations
- new_derivations
的副本并修改了 new_derivations
:
import copy
new_derivations = copy.deepcopy(derivations)
for k, v in derivations.items():
for vi in v:
if (lista_ou_string(vi) and not set(vi).issubset(V1)) or vi not in V1:
if len(v) == 1:
# NEED TO DELETE BOTH KEY AND VALUE FROM derivatios.items()
del new_derivations[k]
break
else:
# NEED TO DELETE ONLY THE VALUE FROM derivations.items()
idx = new_derivations[k].index(vi)
del new_derivations[k][idx]
我实际上会以不同的方式实现上述代码:与其考虑从 derivations
中删除项目,不如考虑何时应将元素添加到列表中。那么代码就变得简单多了:
new_derivations = {}
for k, v in derivations.items():
nv = [vi for vi in v if ((isinstance(vi, list) and set(vi).issubset(V1))
or vi in V1)]
if nv:
new_derivations[k] = nv