python - 使用附加从另一个列表生成一个简单列表

标签 python pymysql

来自这样的列表(original_list):

[[], (['2313240'], 51.0), (['2313301'], 55.0), (['2313467'], 51.0)]

我需要创建另一个仅包含 [0] 元素的列表 (result_list)(例如 2313240),因此我尝试使用以下方法提取它:

for row in range(1, len(original_list)):
    result_list.append(original_list[row][0][0])

但它的结果类似于:

['2313240', '2313301', '2313467']

当我真正想要实现这样的目标时(没有括号的元素):

[2313240, 2313301, 2313467]

因此我可以将它用作查询的参数:

   cursor.execute(sql,[result_list])

现在它记录以下错误:

pymysql.err.InternalError: (1241, 'Operand should contain 1 column(s)')

我不知道如何解决这个问题,因为我是 Python 初学者。

最佳答案

在将值附加到数组之前只需转换为 int:

result_list.append(int(original_list[row][0][0]))

关于python - 使用附加从另一个列表生成一个简单列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037192/

相关文章:

python - 使用 findall 捕获组?

python - 如何使用没有类的 BeautifulSoup 提取值

postgresql - 更新冲突 postgres 上的多个列

python - 尝试将数据插入 MySQL 时出现此错误 ->"TypeError: not all arguments converted during string formatting"

python - 引用pymysql中的记录来搜索一下

python - 安装 python 依赖项不起作用

Python 2.7 子进程

python - `r2_score` 的 `scikit-learn` 与 R^2 计算明显不匹配

mysql - 通过pymysql多进程更新MYSQL一张表

python - MySQL插入语句在Python中不起作用