Python 如何将值附加到尚未存在的键?
我有一本这样的字典:Python 如何将值附加到尚未存在的键?,python,python-3.x,dictionary,append,Python,Python 3.x,Dictionary,Append,我有一本这样的字典: dct = {'one': 'value', 'two': ['value1','value2','value1'], 'three':['otherValue1','otherValue2','otherValue1'], 'dontCareAboutThisKey':'debug'} #remove duplicates no_dups = {k:list(set(v)) if isinstance(v, list) an
dct = {'one': 'value',
'two': ['value1','value2','value1'],
'three':['otherValue1','otherValue2','otherValue1'],
'dontCareAboutThisKey':'debug'}
#remove duplicates
no_dups = {k:list(set(v)) if isinstance(v, list) and k != 'dontCareAboutThisKey' else v
for k,v in dct.items()} # or dct.iteritems() if using python2.x
我需要从列表中删除重复的值。为此,我编写了一个函数:
no_dups = {}
for keys in dct:
if isinstance(dct[keys], list) and keys != 'dontCareAboutThisKey':
for value in dct[keys]:
if value not in no_dups.values():
no_dups[keys].append(value)
else:
no_dups[keys] = dct[keys]
我正在检查当前键的值是否为列表。如果没有,只需将键“复制”到no_dups
字典。如果它是一个列表,而不是我不关心的键(肯定没有重复项),它应该检查当前值是否已经存在于no_dups.values()
中,并将其附加到当前键。问题是我遇到了一个错误:
keyrerror:'two:'
我知道这是因为我试图为不存在的密钥添加一个值,但我不知道如何处理这个问题并使其工作 我认为最好的方法是使用dicts的方法来处理添加键和追加键的问题: 但是,你可以用一种更简洁的方式来做:
dct = {'one': 'value',
'two': ['value1','value2','value1'],
'three':['otherValue1','otherValue2','otherValue1'],
'dontCareAboutThisKey':'debug'}
#remove duplicates
no_dups = {k:list(set(v)) if isinstance(v, list) and k != 'dontCareAboutThisKey' else v
for k,v in dct.items()} # or dct.iteritems() if using python2.x
对于通过
if
测试的键值组合,该黑客会将列表转换为一个集合(删除重复项),然后再转换为一个列表。对于其他键值组合,它将保持原样。我认为处理添加键和同时追加的最佳方法是使用dicts的方法:
但是,你可以用一种更简洁的方式来做:
dct = {'one': 'value',
'two': ['value1','value2','value1'],
'three':['otherValue1','otherValue2','otherValue1'],
'dontCareAboutThisKey':'debug'}
#remove duplicates
no_dups = {k:list(set(v)) if isinstance(v, list) and k != 'dontCareAboutThisKey' else v
for k,v in dct.items()} # or dct.iteritems() if using python2.x
对于通过
if
测试的键值组合,该黑客会将列表转换为一个集合(删除重复项),然后再转换为一个列表。对于其他键值组合,它将保持原样。如果要删除重复项,只需使用set()函数将列表更改为set:
它会自动为您提供唯一的集合,然后您可以随时将其更改回列表。如果要删除重复项,只需使用set()函数将列表更改为set即可:
dct = {'one': 'value',
'two': ['value1','value2','value1'],
'three':['otherValue1','otherValue2','otherValue1'],
'dontCareAboutThisKey':'debug'}
它会自动为您提供唯一的集合,然后您可以随时将其更改回列表
dct = {'one': 'value',
'two': ['value1','value2','value1'],
'three':['otherValue1','otherValue2','otherValue1'],
'dontCareAboutThisKey':'debug'}
set(dct)
返回一个set
,这是一个没有重复项的列表:
for key, value in dct.items():
if not isinstance(value, basestring):
dct[key] = set(value)
如果您需要一本新词典,您可以:
new_dct = {}
for key, value in dct.items():
if not isinstance(value, basestring):
new_dct[key] = set(value)
else:
new_dct[key] = value
set(dct)
返回一个set
,这是一个没有重复项的列表:
for key, value in dct.items():
if not isinstance(value, basestring):
dct[key] = set(value)
如果您需要一本新词典,您可以:
new_dct = {}
for key, value in dct.items():
if not isinstance(value, basestring):
new_dct[key] = set(value)
else:
new_dct[key] = value
哪一行抛出错误?
无重复[keys]。追加(值)
哪一行抛出错误?无重复[keys]。追加(值)
感谢您提供有关集合的信息。我会记住这一点:)谢谢你提供关于集合的信息。我会记住:)我不确定OP是否真的想要创建副本,因为这将在原地编辑。。。但是似乎是一种很好的方法,我不知道set()
。现在容易多了。非常感谢。我想接受两个答案,但这是不可能的。Paulo Bu首先使用了set()
函数,所以我接受他的答案,但我会使用你的,因为这对begginer用户来说并不复杂:)我不确定OP是否真的想要创建副本,因为这将在原地编辑。。。但是似乎是一种很好的方法,我不知道set()
。现在容易多了。非常感谢。我想接受两个答案,但这是不可能的。Paulo Bu首先使用了set()
函数,所以我接受他的答案,但我会使用你的,因为这对begginer用户来说并不复杂:)这很难理解,但我会尝试。谢谢:)太好了,这里没有魔法,Python真的很有理解力,而且很适合练习。我知道,我知道。。但是学习Python
有1500页,我不能读这么快。再次感谢你的回答:)这很难理解,但我会努力的。谢谢:)太好了,这里没有魔法,Python真的很有理解力,而且很适合练习。我知道,我知道。。但是学习Python
有1500页,我不能读这么快。再次感谢您的回答:)