How to use X [:, 0] and X [:, 1] in Python
This article mainly introduces the Python X [:, 0] and X [:, 1] how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you read this Python X [:, 0] and X [:, 1] how to use the article will have a harvest, let's take a look at it.
X [:, 0] is a way of writing an array in numpy, which means that for a two-dimensional array, take all the data in the first dimension of the two-dimensional array, and take the 0th data in the second dimension. Intuitively, X [:, 0] is to take the 0th data of all rows, and X [:, 1] is to take the first data of all rows.
For example: import numpy as np X = np.array ([[0rect 1], [2pje 3], [4jre 5], [6pr 7], [8rect 9], [10rect 11], [12rect 13], [14pr 15], [16pr 17], [18pr 19]]) print X [:, 0]
The output result of X [:, 0] is:
Import numpy as np X = np.array ([[0rem 1], [2je 3], [4jre 5], [6rect 7], [8je 9], [10je 11], [12je 13], [14pr 15], [16pr 17], [18je 19]]) print X [:, 1]
The output result of X [:, 1] is:
X [njournal:] is to take all the values of the element with the subscript n in the first dimension.
X [1] that is, take all the values of the element with subscript 1 in the first dimension, and output the result:
X [:, mpurn], that is, take the m-th to n-Murray 1 column of all data, including the left but not the right
Example: output all rows in the X array from column 1 to column 2 of data X = np.array ([[0Perry 1, 5], [6, 7, 8], [9, 10, 14], [12, 13, 14], [15, 16, 17], [18, 19]) print X [:, 1:3]
Output result:
Add: [1:], [::-1], X [:, mpurn] and X [1JI] in python
[1:] in Python
It means to remove the first element in the list (subscript 0) and operate on the following elements. Take an example, and count the number in the traversal:
Question: if you read the scores of N students, you will output the number of students who get a given score.
Input format:
Enter a positive integer N that does not exceed 10 ^ 5 ^ on line 1, that is, the total number of students. The next line gives the percentile integer scores of N students, separated by spaces. The last line gives the number of scores to query K (positive integers not exceeding N), followed by K fractions, separated by spaces.
Output format:
The number of students whose score is equal to the specified score is given in a row in query order, separated by a space, but there must be no extra spaces at the end of the line.
Stu_num = input ('Please enter the total number of students:') stu_grade = input ('Please enter each student's score (percentile system), separated by spaces:'). Split () # converts the same number of strings into a list num_and_grade = input ('Please enter how many scores to count and each score value Separate by space:') .split () # convert to list format result = [] # define a new list to save the result for i in num_and_grade [1:]: # define variable I Traversing num_and_grade [] list result.append (str (stu_grade.count (I) # using Python's count () function to count the number of corresponding I values in the list stu_grade [] list, convert it to string format and append it to result [] list print (".join (result)) # list into string format, print the result
Results:
Please enter the total number of students: 10
Please enter each student's score (percentile system) and separate it by space: 88 99 75 88 95 42 78 88 95 99
Please enter how many scores you want to count, and each score value, separated by a space: 3 88 99 95
3 2 2
[::-1] in Python
This is the special use of python's slice notation.
B = a [i j] means to copy a [I] to a [j-1] to generate a new list object
When I defaults, the default is 0, that is, a [: 3] is equivalent to a [0:3]
When j defaults, it defaults to len (alist), that is, a [1:] is equivalent to a [1:10]
A [:] is equivalent to a complete copy of a when iMagnej is by default.
B = a [i:j:s] in this format, iMagol j is the same as above, but s means step by step, the default is 1.
So a [i:j:1] is equivalent to a [I: J]
When s