python - 在Python中的元组内将String元素转换为int

标签 python tuples

我有一个元组 a = (('1414', 'A'), ('1416', 'B')) 我需要获取结果 result = ((1414, 'A'), (1416, 'B'))

我尝试了 tuple([map(int, x[0]) for x in a]) 基于 How to convert strings into integers in Python?我得到的结果是 ([1414], [1416])。我如何将结果维护为具有更改值的元组而不是将其作为列表?

最佳答案

使用生成器表达式:

a = (('1414', 'A'), ('1416', 'B'))

result = tuple((int(x[0]), x[1]) for x in a)

关于python - 在Python中的元组内将String元素转换为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702754/

相关文章:

python - 为什么有些小部件不能拉伸(stretch)以填充布局中的最大空间?

python - 用 Python 计算元组的出现次数

python - 多个列表的字典

python - 如何在 Django 中每天准确执行一次命令?

python - 如何使用 pytest 对 sqlalchemy orm 类进行单元测试

list - 递归地将元组列表转换为列表元组

c# - 在 C# 中使用一对(三重等)值作为一个值的最佳方式是什么?

python - 是否可以使用 Google Cloud Datastore 批量插入

python - 如何将列表迭代到元组中

c++ - std::tuple,通过继承类型获取项目