我该如何考虑重复?注意,我在filtered_backup_config中有4个元素,在rendered_config中只有3个元素。但是,我在filtered_backup_config中有一个额外的“测试”元素。当与rendered_config进行比较时,它认为“测试”已经存在,因此minus_commands显示为空。理想的输出应显示minus_commands = ['testing']。
>>> filtered_backup_config = ['hello','world','testing','testing']
>>> rendered_config = ['hello','testing','world']
>>> filtered_set = set(filtered_backup_config)
>>> rendered_set = set(rendered_config)
>>> minus_commands = [x for x in filtered_backup_config if x not in rendered_set]
>>> plus_commands = [x for x in rendered_config if x not in filtered_set]
>>> print minus_commands
[]
>>> print plus_commands
[]
>>>
答案 0 :(得分:1)
使用.AddLogging
:
collections.Counter
UPD:保留订单
>>> from collections import Counter
>>>
>>> filtered_backup_config = ['hello','world','testing','testing']
>>> rendered_config = ['hello','testing','world']
>>> filtered_set = Counter(filtered_backup_config)
>>> rendered_set = Counter(rendered_config)
>>> minus_commands = filtered_set - rendered_set
>>> print minus_commands
Counter({'testing': 1})
>>> print list(minus_commands.elements())
['testing']