gpt4 book ai didi

python - 将字符串列表转换为python中的元组列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:23 26 4
gpt4 key购买 nike

我有一个这种格式的字符串列表:

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

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

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

到目前为止我已经试过了:

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

我得到:

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

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

最佳答案

如果你的字符串是数字的字符串表示,那么:

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

关于python - 将字符串列表转换为python中的元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25478881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com