python子类分配

时间:2021-08-26 03:21:48

原问题是将左边样式变成右边样式:

python子类分配

即有父类和子类,父类包括多个子类,怎样将子类匹配到父类下面的问题

代码如下

 #!/usr/bin/python3.4
# -*- coding: utf-8 -*- arr1 = ["S01","S01","S01","S02","S03","S04","S04"]
arr2 = ["G0001","G0002","G0003","G0004","G0003","G0004","G0005"]
# 用来存放父类
father = []
# 放入第一个单方便后面循环计算
father.append(arr1[0])
# 子类
son = []
# 临时数组
temp = [] for i in range(0,len(arr1)):
# 如果father里面存在,就跳过
# 把子类存入临时数组
if arr1[i] in father:
temp.append(arr2[i])
pass
# 如果father里面不存在
# 释放临时数组
else:
son.append(temp)
temp = []
father.append(arr1[i])
temp.append(arr2[i])
# 将最后一次循环的temp放入son
son.append(temp)
print(father)
print(son)

运行效果:

python子类分配