gpt4 book ai didi

python - 在python中将列表的字符串列表转换为整数

转载 作者:行者123 更新时间:2023-12-02 16:25:52 26 4
gpt4 key购买 nike

我在一个文本文件中有一个数据集,我读取它然后拆分它的行并创建了一个列表列表,例如

[['1 2 3'], ['4 5 6'], ..., ['7 8 9']]

如何将此列表转换为这样的整数列表?

[[1,2,3],[4,5,6],...,[7,8,9]]

最佳答案

这对您有帮助:

lst = [['1 2 3'], ['4 5 6'],['7 8 9']]
lst = [elem.split(' ') for lstt in lst for elem in lstt]
lst = [[int(num) for num in lstt[:]] for lstt in lst]

输出:

>>> lst
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

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

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