python - 获取Python列表的当前索引

标签 python mysql list

我目前有以下内容:

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'}]

但是,我希望在字典中获取某种形式的索引或键。

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'}

最佳答案

你是这个意思吗?

>>> 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'})

关于python - 获取Python列表的当前索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622099/

相关文章:

python - 如何将 240*240*4 的 np 数组打印或转换为 240*240*1?

php - 如何解决 "SQLSTATE[42S22]: Column not found"?

mysql - 设计助手返回零

python - 为什么 python 会改变这个列表中的值?

python - 将 float64 numpy 数组转换为不以科学计数法表示的 float

python - 如何将 Django 数据库中的模板标签解释/渲染为 HTML

c# - 使用C#连接MYSQL数据库

python - 如何打印每个列表的第一个字符,然后打印下一个字符

c# - 如何使用 linq 将 'Y' 或 'N' 值转换为 bool 值?

python - Bash重定向编码错误