如何将int列表转换为列表列表?(How to convert list of ints into list of lists?)

编程入门 行业动态 更新时间:2024-10-28 15:34:05
如何将int列表转换为列表列表?(How to convert list of ints into list of lists?)

问题是:如何正确地(以“pythonic方式”)将整数列表[1,3,7,6 [1, 3, 7, 6]转换为相应的列表列表: [[1], [3], [7], [6]]在他们内部注入?

试图这样做: unsorted_list = [list(str(x)) for x in unsorted_list]但得到[['1'], ['3'], ['7'], ['6']] 。 不知道如何在整数上执行相同操作,它们不可迭代。 之后使用可以解决问题,但不是那么优雅。 谢谢。

The problem is: how to convert properly (in "pythonic way") list of ints: [1, 3, 7, 6] into corresponding list of lists: [[1], [3], [7], [6]] with ints inside of them?

Tried to do it like: unsorted_list = [list(str(x)) for x in unsorted_list] but get [['1'], ['3'], ['7'], ['6']]. Don't know how to perform the same on ints, they are not iterable. Using for afterwards can solve the problem, but not so elegant. Thank you.

最满意答案

您不应该将整数转换为字符串。 只是:

[[x] for x in unsorted_list]

You shouldn't transform the integers to strings. Simply:

[[x] for x in unsorted_list]

更多推荐

本文发布于:2023-08-03 07:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   转换为   如何将   int   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!