【发布时间】:2018-07-11 18:25:47
【问题描述】:
我从 mysql 数据库的表中获取数据。 我正在尝试过滤具有特定值的字典列表。当我执行代码时,出现以下错误:
使用 IDE = PyCharm。
错误:
[x.append(k) for k in lst_cmp_helpdesk_dic if "Doble" in k[key]]
TypeError: 'int' 类型的参数不可迭代
import mysql.connector
def test_initialize_test_data():
global lst_cmp_helpdesk
global lst_cmp_helpdesk_dic
cnx = mysql.connector.connect(user='User', password='Password', host='Srv Name or IP', database='DataBase Name')
try:
cursor_dic = cnx.cursor(dictionary=True,buffered=True)
cursor_dic.execute("select * from Table_Name")
lst_cmp_helpdesk_dic = cursor_dic.fetchall()
x = []
for key in lst_cmp_helpdesk_dic[0].keys():
[x.append(k)for k in lst_cmp_helpdesk_dic if "Search_Text" in k[key]]
count = len(([dict(t) for t in set([tuple(d.items()) for d in x])]))
print("\n Length = " + str(count))
finally:
cnx.close()
【问题讨论】:
-
相反,
k是一个整数,尝试使用[key]进行索引是无效的 -
啊,你是对的 - 这会给出一个
not subscriptable错误