如何为 python 中 OrderedDict 列表中的重复值抛出 AssertionError
How to throw AssertionError for duplicate values in list of OrderedDict in python
我有一个 OrderedDict 列表。 OrderedDict 是 key/value 对的元组:
dict_lists = [[OrderedDict([('name', 'Good check'), ('url',
'https://www.facebook.com/'), ('lag', 10), ('tags',
['team:team 4', 'section 1', 'section 2']), ('headers',
OrderedDict([('Location', 'http://facebook.com/')]))]),
OrderedDict([('name', 'Bad check'), ('url',
'https://www.google.com/'), ('lag', 5), ('tags',
['team:team 1'])]), OrderedDict([('name', 'Bad check'),
('url', 'https://www.microsoft.com/'), ('lag', 5), ('tags'
, ['team:team 5'])])]]
当 "name"
键有重复值时,我试图抛出 AssertionError。
目前,它适用于键,因为我相信它会为重复键抛出 AssertionError:
AssertionError: 'name' unexpectedly found in ['name', 'url', 'lag',
'tags', 'headers']
重复键的代码:
import unittest
from collections import OrderedDict
keys = []
for dict_list in dict_lists:
for dictionary in dict_list:
dict_keys = dictionary.keys()
for key in dict_keys:
self.assertNotIn(key, keys)
keys.append(key)
如何让它对重复值起作用(仅针对 "name"
键)
将该键的值存储在一个集合中,然后对该集合进行测试:
name_values = {}
for dict_list in dict_lists:
for dictionary in dict_list:
if 'name' in dictionary:
value = dictionary['name']
self.assertNotIn(value, name_values)
name_values.add(value)
您真的也应该使用一套钥匙。也不需要调用 keys()
方法,遍历字典直接给你键。
如果你想将循环组合在一起,你可以只测试当前键是否等于 'name'
:
keys = {}
name_values = {}
for dict_list in dict_lists:
for dictionary in dict_list:
for key in dictionary:
self.assertNotIn(key, keys)
keys.add(key)
if key == 'name':
value = dictionary[key]
self.assertNotIn(value, name_values)
name_values.add(value)
请注意,您的词典已排序这一事实对此处的解决方案没有影响。
我有一个 OrderedDict 列表。 OrderedDict 是 key/value 对的元组:
dict_lists = [[OrderedDict([('name', 'Good check'), ('url',
'https://www.facebook.com/'), ('lag', 10), ('tags',
['team:team 4', 'section 1', 'section 2']), ('headers',
OrderedDict([('Location', 'http://facebook.com/')]))]),
OrderedDict([('name', 'Bad check'), ('url',
'https://www.google.com/'), ('lag', 5), ('tags',
['team:team 1'])]), OrderedDict([('name', 'Bad check'),
('url', 'https://www.microsoft.com/'), ('lag', 5), ('tags'
, ['team:team 5'])])]]
当 "name"
键有重复值时,我试图抛出 AssertionError。
目前,它适用于键,因为我相信它会为重复键抛出 AssertionError:
AssertionError: 'name' unexpectedly found in ['name', 'url', 'lag', 'tags', 'headers']
重复键的代码:
import unittest
from collections import OrderedDict
keys = []
for dict_list in dict_lists:
for dictionary in dict_list:
dict_keys = dictionary.keys()
for key in dict_keys:
self.assertNotIn(key, keys)
keys.append(key)
如何让它对重复值起作用(仅针对 "name"
键)
将该键的值存储在一个集合中,然后对该集合进行测试:
name_values = {}
for dict_list in dict_lists:
for dictionary in dict_list:
if 'name' in dictionary:
value = dictionary['name']
self.assertNotIn(value, name_values)
name_values.add(value)
您真的也应该使用一套钥匙。也不需要调用 keys()
方法,遍历字典直接给你键。
如果你想将循环组合在一起,你可以只测试当前键是否等于 'name'
:
keys = {}
name_values = {}
for dict_list in dict_lists:
for dictionary in dict_list:
for key in dictionary:
self.assertNotIn(key, keys)
keys.add(key)
if key == 'name':
value = dictionary[key]
self.assertNotIn(value, name_values)
name_values.add(value)
请注意,您的词典已排序这一事实对此处的解决方案没有影响。