List array parsing (finally a little clearer)

Source: Internet
Author: User
Tags true true

#-*-coding:utf-8-*-"""Created on Tue 23:04:51 2016@author:administrator"""ImportNumPy as NP" "The list in Python is Python's built-in data type, and the data classes in the list do not have to be the same, and the types in the array must all be the same. The data type in the list holds the address of the data, simply the pointer, not the data, so it is too troublesome to save a list, for example, list1=[1,2,3, ' a '] requires 4 pointers and four data, which increases the storage and consumption of the CPU. The array in the NumPy is powerful, with the same data types stored in it." "#Module 1 Array list basic displaylist1=[1,2,3,'a']  PrintList1 a=np.array ([1,2,3,4,5]) b=np.array ([[1,2,3],[4,5,6]]) C=list (a)#array-to-list conversionsPrintA,np.shape (a)PrintB,np.shape (b)PrintC,np.shape (c)" "[1, 2, 3, ' a '][1 2 3 4 5] (5L,) [[1 2] [3 4 5]] (2L, 3L) [6, 1, 2, 3, 4] (5L,)" "" "whosvariable type Data/info-------------------------------a ndarray 5l:5 elems, type ' int32 ', 20         Bytesb ndarray 2lx3l:6 Elems, type ' int32 ', Bytesc list n=5list1 list N=4NP Module <module ' NumPy ' from ' C:\<...>ages\numpy\__init__.pyc ' >" "#Module 2#Create: The creation of an array: The parameter can be either a list or a tuple. Use corresponding properties shape to get shape directlyPrint '222222222222222222222222222222222222222222\n'a=np.array ((1,2,3,4,5))#parameter is a tupleB=np.array ([6,7,8,9,0])#parameter is ListC=np.array ([[1,2,3],[4,5,6]])#parameter two-dimensional arrayPrintA, bPrintC.shape#[1 2 3 4 5] [6 7 8 9 0] (2L, 3L) #print A, B,#[1 2 3 4 5] [6 7 8 9 0]#(2L, 3L) Note: After print, add \ n newline; Note: Print a, B, no, no, wrap, if there is, do not break the line#Module 3 can also directly change the shape of the property array, 1 represents the self-calculationPrint '33333333333333333333333333333333333333333333\n'C= Np.array ([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])  PrintC.shape#(3L, 4L)C.shape=4,-1PrintCc.reshape (2,-1)  PrintCPrint 'reshape to generate new variables'x= C.reshape (2,-1)  Printx" "33333333333333333333333333333333333333333333 (3L, 4L) [[1 2 3] [4 4 5] [6 7 7] [8 9 10]][[1 2 3] [4 4 5 ] [6 7 7] [8 9 10]]reshape generate a new variable [[[1 2 ' 3 4 4 5] [6 7 7 8 9]]" "" "The reshape is ultimately a shallow copy, which means that the same memory space is used for the original book C ." "X[0]=1000PrintxPrintC" "33333333333333333333333333333333333333333333 (3L, 4L) [[1 2 3] [4 4 5] [6 7 7] [8 9 10]][[1 2 3] [4 4 5    ] [6 7 7] [8 9 10]]reshape generate a new variable [[[1 2 ' 3 4 4 5] [6 7 7 8 9 10]][[1000 1000 1000 1000 1000 1000] [6 7 7 8 9 10]][[1000 [6 7 7] [8 9]]" "#Module 4 does not use data types when creating arrays, we can also use data types here. The default is Int32.A1=np.array ([[1,2,3],[4,5,6]],dtype=Np.float64)PrintA1.dtype,a.dtype#float64 Int32#Module 5 in front of the creation of the time we are all using the Np.array () method from a tuple or list to convert into an array, it is very laborious, NumPy himself provides a lot of methods to let us directly create an array.Arr1=np.arange (1,10,1) arr2=np.linspace (1,10,10)  PrintArr1,arr1.dtypePrintArr2,arr2.dtype" "[1 2 3 4 5 6 7 8 9] int32[1.   2.3.   4.5.   6.7.  8.9. Ten.] Float64np.arange (A,B,C) indicates that an array is generated from a-B, with an interval of C, and the data type is int32 by default. But Linspace (a,b,c) says that the A-B is divided into C-points, which includes B. " "#Module 6 Sometimes we need to give different values to the coordinates of each element, you can use the FromFunction functionPrint '666666666666666666\n'defFun (i):returnI%4+2PrintNp.fromfunction (Fun, (10,)) #[2.3.  4.5.  2.3.  4.5. 2.3.]deffun2 (i,j):return(i+1) * (j+1) PrintNp.fromfunction (fun2, (9,9))  " "Although there are a lot of ways to produce arrays directly, in most cases we will be converting from list, because in the actual processing we need to load the file from TXT so that the directly read-in data display is stored in the list. Need to deal with when we switch to array, because the design of the array is more in line with our use, involving the matrix operation in the use of mat, then the list is mainly used for elements of the request. " "defLoaddataset (fileName): File=Open (fileName) Datamat=[]      forLineinchfile.readlines (): CurLine=line.strip (). Split ('\ t') Floatline=map (Float,curline)#The map function is used here to convert the data directly into the float type.datamat.append (floatline)returnDatamat#The final data returned above is the original list dataset, which is converted to an array or mat depending on the processing requirements. In fact, the array is the parent of the mat, can use the mat place, the array can theoretically be passed in. #Module 7 element access:arr = Np.array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])Print '77777777777777777'" "one-dimensional array method arr[5] #5 Arr[3:5] #array ([3, 4]) Arr[:5] #array ([0, 1, 2, 3, 4]) arr[:-1]# Array ([0, 1, 2, 3, 4, 5, 6, 7, 8]) arr[:] #array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr[2:4]=100 # Array ([0, 1, 100, 100    , 4, 5, 6, 7, 8, 9]) Arr[1:-1:2] #array ([1, 100, 5, 7]) 2 is the interval arr[::-1] #array ([9, 8, 7, 6, 5, 4, 1, 0]) arr[5:2:-1]#-1 intervals represent right-to-left so 5>2 #array ([5, 4, +])" "#Let's take a look at the two-dimensional approach.c = Np.array ([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])  PrintC[1]  PrintC[1:2] PrintC[1][2]  PrintC[1:4]  PrintC[1:4][0][2] " "[4 5 6 7][[4 5 6 7]]6[[4 5 6 7] [7 8 9 10]]6 can be seen to have: the expression of the final result outside also nested a layer list of [],. Access must note that Python is the most bug, the syntax is flexible, regardless of the writing index syntax is correct, but the final book results will surprise you. The index of the array will eventually result in a shallow copy of the original data and a shared memory with the original data ." "b=arr[1:6] b[:3]=0Printb#the reason for the above is because the data stored directly in the array, the copy of the word directly take away is pointer, not take away the data, but the list will directly occur deep copy, the data pointer all withlist1=list (c) list1[1]=0PrintList1#0 of the changes above have not been changedPrintC#[Array ([1, 2, 3, 4]), 0, Array ([7, 8, 9, ten])]#Module 8" "In addition to these there are their own more cattle break way (only with array) 1) use a Boolean array. Feel very strong, do not write your own judgment statement, note that this way to get results and the original array shared space. Boolean indexes only apply to arrays array,list not eligible. The Boolean index eventually gets the data with the subscript index true. Index can only be a Boolean array" "Print '888888888888\n'a=np.array ((1,2,3,4,5))#parameter is a tupleA=np.array (a*2)  PrintA[A&GT;5]#[6 8]PrintA>5#[False False true true]#list indexes can be arrays and lists. The returned data does not share memory with the original data. The index can be list and arrayX=np.arange (10) Index=[1,2,3,4,5] Arr_index=Np.array (Index)PrintxPrintX[index]#List IndexPrintX[arr_index]#array indexPrint '8-1'a=np.arange (10) Lista=List (a)PrintA*2PrintLista*2#the product of an array and a list is scary." "[0 2 4 6 8 18][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 , 2, 3, 4, 5, 6, 7, 8, 9]" "#Module 9 broadcast of arrayPrint '9\n'a= Np.arange (0, ten). Reshape (-1, 1) b= Np.arange (0, 5)  PrintaPrintbPrintNp.add (A, B)

List array parsing (finally a little clearer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.