我對如何使用for循環創建一些字典並將這些字典追加到列表有點困惑。使用FOR LOOP的字典
object_1 = dict()
object_1['ID'] = 1234
object_1['NAME'] = 'ACB'
...
object_2 = dict()
object_2['ID'] = 123
object_2['NAME'] = 'ABC'
...
object_list = list()
object_list.append(object_1)
object_list.append(object_2)
我想創建X字典對象,其中X是從我選擇的行數。
cur = conn.cursor()
cur.execute("SELECT * FROM account WHERE date=current_date")
rows = cur.fetchall()
i=0
for row in rows:
...
#What I thought
i += 1
obj_+i = dict()
obj_+i['ID'] = row[0]
....
....
obj_list = list()
obj_list.append(obj_+i)
你是否真的縮進看起來像嗎? –
這只是一個例子:] – TMoraes
我想你會受益於閱讀https://nedbatchelder.com/text/names.html – chepner