python - 将字符串列表转换为 2 元组整数列表
问题描述
我目前有以下列表:['10 90', '10 -90', '100 45', '20 180']
。我正在尝试将字符串列表转换为充满整数的 2 元组,例如[(10, 90), (10, -90), (100, 45), (20, 180)]
.
我试过使用这段代码:
res = [tuple(map(int, sub.split(', '))) for sub in test_list]
但是,我收到此错误:
ValueError: invalid literal for int() with base 10: '10 90'
Command exited with non-zero status 1
任何帮助将不胜感激。
解决方案
尝试这个:
lst = ['10 90', '10 -90', '100 45', '20 180']
res = [tuple(map(int, x.split())) for x in lst]
print(res)
输出:
[(10, 90), (10, -90), (100, 45), (20, 180)]
推荐阅读
- amazon-web-services - AWS cloudwatch cron 表达式问题
- javascript - 是否有正确的方法可以单击组件标签以滚动到该页面?
- java - 使自定义视图使用布局权重
- reactjs - easy-peasy:无法调用或调度操作
- visual-studio-code - 如何将新网页添加到 VSC liveserver?
- python - 如何在 Python 中使用 lxml 查找嵌套的 html 元素?
- javascript - 即使 Gremlin 查询无法完成,如何返回遍历的所有步骤?
- reactjs - Redux-React-Firebase 模拟测试
- javascript - 离子溢出滚动干扰 $ionScrollDelegate
- python - 从列表中获取具有约束的多个非重复随机连续元素组