将字符串列表转换为python中的元组列表 [英] Convert a list of strings to a list of tuples in python

查看:41
本文介绍了将字符串列表转换为python中的元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的字符串列表:

I have a list of strings in this format:

['5,6,7', '8,9,10']

我想将其转换为以下格式:

I would like to convert this into the format:

[(5,6,7), (8,9,10)]

到目前为止,我已经尝试过:

So far I have tried this:

[tuple(i.split(',')) for i in k]

我得到:

[('5','6','7'), ('8','9','10')]

我对如何将字符串简单地转换为整数元组有些困惑.谢谢

I am a bit stuck on how to simply convert the strings into tuples of integers. Thank you

推荐答案

如果您的字符串是数字的字符串表示形式,则:

If your strings are strings representation of number, then:

[tuple(int(s) for s in i.split(',')) for i in k]

这篇关于将字符串列表转换为python中的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆