将字符串列表转换为整数嵌套列表

Convert a list of strings to a Nested list of integers

提问人:Francis 提问时间:10/31/2022 最后编辑:Andrej KeselyFrancis 更新时间:10/31/2022 访问量:46

问:

我有这个输出:['0100', '0100'],我想有这样的东西[[0,1,0,0],[0,1,0,0]]。

我该怎么办?

python 字符串 嵌套列表

评论


答:

4赞 Andrej Kesely 10/31/2022 #1

尝试:

lst = ["0100", "0100"]

out = [[int(ch) for ch in s] for s in lst]
print(out)

指纹:

[[0, 1, 0, 0], [0, 1, 0, 0]]

评论

2赞 flakes 10/31/2022
或者[list(map(int, s)) for s in lst]