Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我已经生成了一个大小为 6“乘 1”的列表。但我想将列表分成 3 个“列”,以便大小应该是 2“乘 3”。我怎样才能做到这一点?
例如:
[1,1,2,2,3,3] # -> [[1,1],[2,2],[3,3]]
假设您当前的列表称为flat:
flat
col_width = len(flat) // 3 nested = [flat[i:i+col_width] for i in range(0, len(flat), col_width)]