How to create list of items in a list by index in python
本问题已经有最佳答案,请猛点这里访问。
我有一张单子
1 | m = ['ABC', 'XYZ', 'LMN'] |
我希望输出如下:
1 2 3 | m = [['a','x','l'] ['b','y','m'] ['c','z','n']] |
怎么能做到?
你所需要的只是一个列表理解,
1 2 3 | > m=['ABC','XYZ','LMN'] > [list(map(str.lower, sub)) for sub in zip(*m)] [['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']] |
使用
1 | print(list(zip(*[list(i.lower()) for i in m]))) |
输出:
1 | [('a', 'x', 'l'), ('b', 'y', 'm'), ('c', 'z', 'n')] |
如果希望子值为列表:
1 | print(list(map(list,zip(*[list(i.lower()) for i in m])))) |
输出:
1 | [['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']] |
1 2 3 4 5 6 7 8 9 10 11 12 | m=['ABC','XYZ','LMN'] import numpy as np new_list = [[0, 0, 0], [0, 0,0],[0,0,0]] j = 0 for i in m: a = list(i.lower()) print(a) new_list[j] = a j = j+1 np.transpose(new_list).tolist() |
输出:
1 | [['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']] |