我目前有以下内容:
jsonfiletext = jsonfile.read()
data = json.loads(jsonfiletext)
cursor.execute("SELECT CheckinID FROM checkins ORDER BY CheckinID DESC LIMIT 1")
for row in cursor.fetchall():
checkin_id = row[0]
for checkin in data:
~do stuff~
数据如下所示:
[{u'abc': u'123', u'def': u'456'}, {u'abc': u'789', u'def': u'012'}]
但是,我希望在dict上获得某种形式的索引或密钥。
jsonfiletext = jsonfile.read()
data = json.loads(jsonfiletext)
cursor.execute("SELECT CheckinID FROM checkins ORDER BY CheckinID DESC LIMIT 1")
for row in cursor.fetchall():
checkin_id = row[0]
for checkin in data:
if checkin.index() > checkin_id:
~do stuff~
基本上,我想根据我在数据库中已有的内容避免重复。
checkin.index()将如下:
1. {u'abc': u'123', u'def': u'456'}
2. {u'abc': u'789', u'def': u'012'}
答案 0 :(得分:1)
你是说这个吗?
>>> my_dict = {'age': 18, 'name': 'jhon', 'sex': 'm'}
>>> for index,item in enumerate(my_dict.items()):
print index, dict([item])
0 {'age': 18}
1 {'name': 'jhon'}
2 {'sex': 'm'}
编辑:
>>> data = [{u'abc': u'123', u'def': u'456'}, {u'abc': u'789', u'def': u'012'}]
>>> for item_with_index in enumerate(data):
print item_with_index
(0, {u'abc': u'123', u'def': u'456'})
(1, {u'abc': u'789', u'def': u'012'})