Python - FOR循环如何在python中工作

AJA*_*JAY 1 python

我在Python中有以下代码,我不确定这里的for循环是如何工作的.

cur.execute("SELECT CUST_ID, COMPANY, LASTNAME, CITY, STATE FROM 
mytable")                            

colnames = [desc[0] for desc in cur.description]      
Run Code Online (Sandbox Code Playgroud)

这是如何工作的?

[desc[0] for desc in cur.description]   
Run Code Online (Sandbox Code Playgroud)

什么是desc[0]?

小智 5

基于你给我们的代码,它似乎desc是一个数组本身,因此colnamesdesc[0]你从每个desc中获得的所有数组cur.description

desc[0] 是desc中的第一个项目,因为Python中的数组是零索引的.