, , ['key']? , data1 ['key'] = 'hello', , data2 ['key'] = 'hello' ? , ? , . ( "list" , python)
datalist = [datadict1, datadict2, datadict3]
big_key_list = []
for datadict in datalist:
curkey = datadict.get('key')
if curkey not in big_key_list:
big_key_list.append(curkey)
else:
raise Exception("Key %s in two data dicts" % curkey)
- , dict, , . , , , , ( , , - , ).
EDIT:
, , , , , . , , . .
:
class MyDataObject(object):
def __init__(self, **kwargs):
for k,v in kwargs:
self.__dict__[k] = v
4 :
class MyDataObject(object):
def __init__(self, timestamp, action, obj_type, obj_id):
self.timestamp = timestamp
self.action = action
self.type = obj_type
self.id = obj_id
.
data = {}
data['key1'] = MyDataObject(timestamp='some timestamp', action='some action', type='some type', id = 1234)
data['key2'] = MyDataObject(timestamp='some timestamp2', action='some action2', type='some type2', id = 1235)
, :
data['key1'].timestamp
data['key2'].action
dict() (, , x = 'action ", ).
data['key1'].__dict__('action')
data['key2'].__dict__('timestamp')
, , , , ( MyDataObject).