这个是和《Python 取三维列表特定行列页的元素》一起写的,因为最近处理数据涉及到三维list的存取问题,在python 中没有找到更为合适的数据结构,不得已采用list。这个函数主要利用了python 浅拷贝的特性,有时候浅拷贝会带来灾难,但是有时候浅拷贝会让事情变得如此简单。
def rmDataFrom3list(Data,a=[],b=[],c=[]):
"""
remove the specfic elements of specfic raw, cloum, page in 3D-list.
> @param[in] Data: The 3D-list
> @param[in] a: The raw index
> @param[in] b: The column indx
> @param[in] c: The page indx
return:
< @param[out] Data the 3D-list after remove the elements
"""
z=len(Data) #page
h=len(Data[0]) #raw
l=len(Data[0][0]) #column
tmp1=[]
tmp2=[]
if len(a)==0:
a=list(range(0,h))
if len(b)==0:
b=list(range(0,l))
if len(c)==0:
c=list(range(0,z))
a=list(reversed(a))
b=list(reversed(b))
c=list(reversed(c))
for i in c:
hldata=Data[i]
for j in a:
ldata=hldata[j]
for k in b:
del(ldata[k])
return Data
使用示例:
c=[[[111,121,131,141],
[211,221,231,241],
[311,321,331,341]],
[[112,122,132,142],
[212,222,232,242],
[312,322,332,342]],
[[113,123,133,143],
[213,223,233,243],
[313,323,333,343]]]
print(c)
d=gr.rmDataFrom3list(c,[0,1,2],[2],[0,1,2])
print(d)
返回结果:
[[[111, 121, 141],
[211, 221, 241],
[311, 321, 341]],
[[112, 122, 142],
[212, 222, 242],
[312, 322, 342]],
[[113, 123, 143],
[213, 223, 243],
[313, 323, 343]]]
样例数据是一个3*4*33行4列3页的列表。删除的元素是每一页每一行第三列的元素。
它对应的matlab的功能是m(i,j,k)=[](i,j,k可以是切片)。
1106



被折叠的 条评论
为什么被折叠?



