Python – 在 K 切片时将 2D 列表转换为 3D
有时,在使用Python列表时,我们可能会遇到需要在每个 Kth 列表中将 2D 列表转换为 3D 的问题。这类问题比较特殊,但可以在各种数据域中应用。让我们讨论可以执行此任务的某些方式。
Input : test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]] , K = 3
Output : [[[6, 5], [2, 3], [3, 1]], [[4, 6], [3, 2], [1, 6]]]
Input : test_list = [[6, 5], [2, 3], [3, 1]] , K = 1
Output : [[[6, 5]], [[2, 3]], [[3, 1]]]
方法#1:使用循环
这是可以执行此任务的粗暴方式。在此,我们遍历每个元素,并在每个 Kth 子列表中维护一个计数器,创建一个新列表并相应地追加。
# Python3 code to demonstrate working of
# Convert 2D list to 3D at K slicing
# Using loop
# initializing list
test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 2
# Convert 2D list to 3D at K slicing
# Using loop
res = []
subl = []
cnt = 0
for sub in test_list:
subl.append(sub)
cnt = cnt + 1
if cnt >= K:
res.append(subl)
subl = []
cnt = 0
# printing result
print("Records after conversion : " + str(res))
输出 :
The original list is : [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
Records after conversion : [[[6, 5], [2, 3]], [[3, 1], [4, 6]], [[3, 2], [1, 6]]]
方法 #2:使用zip()
+ 列表理解
上述功能的组合也可以用来解决这个问题。在此,我们通过首先根据大小对值进行分块来执行任务,然后将列表理解与 zip() 一起用于维度转换。
# Python3 code to demonstrate working of
# Convert 2D list to 3D at K slicing
# Using zip() + list comprehension
# initializing list
test_list = [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 2
# Convert 2D list to 3D at K slicing
# Using zip() + list comprehension
test_list = iter(test_list)
temp = [test_list] * K
res = [list(ele) for ele in zip(*temp)]
# printing result
print("Records after conversion : " + str(res))
输出 :
The original list is : [[6, 5], [2, 3], [3, 1], [4, 6], [3, 2], [1, 6]]
Records after conversion : [[[6, 5], [2, 3]], [[3, 1], [4, 6]], [[3, 2], [1, 6]]]