python - 我如何从这个字符串中挑选?

标签 python string list typeerror python-2.5

查询

SELECT leaveZoneVelocity, leaveZoneVelocityZ, leaveZoneVelocityXYZ
FROM stageentertimes WHERE player_id=? AND map_id=? ORDER BY duration ASC

代码:

leavestart_t1_all = [{u'leaveZoneVelocityZ': 0L, u'leaveZoneVelocityXYZ': 245L, u'leaveZoneVelocity': 245L}]

leavestart_t1 = leavestart_t1_all["leaveZoneVelocity"]

leavestart_t1 = leavestart_t1_all[3]

我得到了第一个leave_start_t1

TypeError: list indices must be integer

第二个:

List index out of range

希望你能帮助我,也许能给我一个解释。

最佳答案

您有一个包含一个字典的列表。索引列表,然后返回字典:

leavestart_t1 = leavestart_t1_all[0]["leaveZoneVelocity"]

这里leavestart_t1_all[0]索引外部列表,返回包含的字典。

如果您使用的是 SQL 库,则可以使用 cursor.fetch_one() 来获取第一行,而不是使用 cursor.fetch_all()然后需要建立索引。

关于python - 我如何从这个字符串中挑选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28527654/

相关文章:

list - 如何增强小型 Haskell 代码片段

c# - 字典列表与嵌套字典

python - Django:删除未使用的文件

python - 访问 Numpy 数组中中间四个元素的最快方法?

python - 无法调试 AWS SAM hello world 应用程序

Java字符串数组初始化循环结构内外

c++ - 如何在 C++ 中处理不同的模板参数?

python - 如何更新词典及其列表内容?

python - 如何使用python转义xml属性中的特殊字符

c - 字符串和用户在 C 中输入的字符串有什么区别