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

标签 python

<分区>

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

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

colnames = [desc[0] for desc in cur.description]      

这是如何工作的?

[desc[0] for desc in cur.description]   

什么是desc[0]?

最佳答案

根据您给我们的代码,desc 本身似乎是一个数组,因此 colnames 是所有 desc[0] 的数组 你从 cur.description

中的每个 desc 得到

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

关于Python - FOR 循环如何在 python 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49460972/

相关文章:

python - on_delete 参数的多个值

python - 使用旧版本的 NumPy 格式化 2D 数组

python - 使用云端硬盘文件流获取Google云端硬盘文件链接

python - 如何在笔记本选项卡pygtk中获取输入字段作为密码(*)

python - 特定任务后 celery 关闭 worker

python - 如何在 python 中找到 numpy 矩阵的长度(或尺寸、大小)?

python - 等到元素不出现 - Selenium

python - 多处理池生成初始化非常慢,16 个进程需要 100 秒

python - 我如何将 NaNs 归因为 means() 但通过多索引

python - 跟踪图像时迭代陷入循环(无限循环)