You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python:如何将元组列表转换为无引号的整数列表?

生成目标整数列表的解决方案

你当前的代码通过', '.join(...)将所有索引字符串拼接成单个字符串,最终得到包含一个字符串元素的列表['0,1,2']。要直接生成整数列表[0,1,2],不需要拼接字符串,直接对每个元组的索引部分做类型转换即可,有两种常用实现方式:

方法一:列表推导式(推荐)

selected_listbox_select_criteria_column = [('0', 'firstName'),('1', 'lastName'),('2', 'phone')]
column_to_check_for_criteria = [int(elem[0]) for elem in selected_listbox_select_criteria_column]

遍历每个元组,提取第一个元素(字符串类型的索引)并转换为整数,最终组成目标整数列表。

方法二:使用map函数

selected_listbox_select_criteria_column = [('0', 'firstName'),('1', 'lastName'),('2', 'phone')]
column_to_check_for_criteria = list(map(lambda x: int(x[0]), selected_listbox_select_criteria_column))

通过map对每个元组执行索引提取和类型转换,再将结果转为列表。

两种方法执行后,column_to_check_for_criteria的值均为[0,1,2],完全符合你的需求。

内容的提问来源于stack exchange,提问作者Nico44044

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 02:23:15