假设我有一个列表列表;
my_list = [['apple', 'banana', 'orange'], ['carrot', 'pumpkin', 'potato']]
有没有办法可以将每个单独列表中的每个元素转换为自己的列表?
输出:
new_list = [[['apple'], ['banana'], ['orange']], [['carrot'], ['pumpkin'], ['potato']]]
是的,您可以使用列表推导将每个字符串包装在自己的列表中:
[[[item] for item in sublist] for sublist in my_list]
这输出:
[[['apple'], ['banana'], ['orange']], [['carrot'], ['pumpkin'], ['potato']]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句