## Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]

2020年02月18日 编程语言 ⁄ 共 2030字 ⁄ 字号 评论关闭

Python中对于数组和列表进行切片操作是很频繁的，当然对于切片的操作可供我们直接使用的函数也是很遍历了，我们今天主要简单总结一下常用集中索引化方式，希望对大家有所帮助吧。

#!usr/bin/env python#encoding:utf-8from __future__ import pision'''__Author__:沂水寒城学习Python中的X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n]'''import numpy as npdef simple_test(): ''' 简单的小实验 ''' data_list=[[1,2,3],[1,2,1],[3,4,5],[4,5,6],[5,6,7],[6,7,8],[6,7,9],[0,4,7],[4,6,0],[2,9,1],[5,8,7],[9,7,8],[3,7,9]] # data_list.toarray() data_list=np.array(data_list) print 'X[:,0]结果输出为：' print data_list[:,0] print 'X[:,1]结果输出为：' print data_list[:,1] print 'X[:,m:n]结果输出为：' print data_list[:,0:1] data_list=[[[1,2],[1,0],[3,4],[7,9],[4,0]],[[1,4],[1,5],[3,6],[8,9],[5,0]],[[8,2],[1,8],[3,5],[7,3],[4,6]], [[1,1],[1,2],[3,5],[7,6],[7,8]],[[9,2],[1,3],[3,5],[7,67],[4,4]],[[8,2],[1,9],[3,43],[7,3],[43,0]], [[1,22],[1,2],[3,42],[7,29],[4,20]],[[1,5],[1,20],[3,24],[17,9],[4,10]],[[11,2],[1,110],[3,14],[7,4],[4,2]]] data_list=np.array(data_list) print 'X[:,:,0]结果输出为：' print data_list[:,:,0] print 'X[:,:,1]结果输出为：' print data_list[:,:,1] print 'X[:,:,m:n]结果输出为：' print data_list[:,:,0:1]if __name__ == '__main__': simple_test()

X[:,0]结果输出为：[1 1 3 4 5 6 6 0 4 2 5 9 3]X[:,1]结果输出为：[2 2 4 5 6 7 7 4 6 9 8 7 7]X[:,m:n]结果输出为：[[1][1][3][4][5][6][6][0][4][2][5][9][3]]X[:,:,0]结果输出为：[[ 1 1 3 7 4][ 1 1 3 8 5][ 8 1 3 7 4][ 1 1 3 7 7][ 9 1 3 7 4][ 8 1 3 7 43][ 1 1 3 7 4][ 1 1 3 17 4][11 1 3 7 4]]X[:,:,1]结果输出为：[[ 2 0 4 9 0][ 4 5 6 9 0][ 2 8 5 3 6][ 1 2 5 6 8][ 2 3 5 67 4][ 2 9 43 3 0][ 22 2 42 29 20][ 5 20 24 9 10][ 2 110 14 4 2]]X[:,:,m:n]结果输出为：[[[ 1] [ 1] [ 3] [ 7] [ 4]][[ 1] [ 1] [ 3] [ 8] [ 5]][[ 8] [ 1] [ 3] [ 7] [ 4]][[ 1] [ 1] [ 3] [ 7] [ 7]][[ 9] [ 1] [ 3] [ 7] [ 4]][[ 8] [ 1] [ 3] [ 7] [43]][[ 1] [ 1] [ 3] [ 7] [ 4]][[ 1] [ 1] [ 3] [17] [ 4]][[11] [ 1] [ 3] [ 7] [ 4]]][Finished in 0.6s]