问题是:如何正确地(以“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]更多推荐
发布评论