Some common things

1.numpy.random.uniform(low,high,size)

For example: numpy.random.uniform (-0.25,0.25,300) randomly generated between size dimension [-0.25,0.25) is nparray random initialization vector 300 words are better between -0.25,0.25 papers say

2.Python built difference range () and in arange Numpy () function

Range (5) only one parameter from zero by default the step size output 0,1,2,3,4

Range (1,5) 1,2,3,4 then the output from the start step of 1 1

Range (1,5,2) at this time starts from step 1 to the output 2, 3. Note written range (5,2) and not on behalf of from 0 to 5 in steps of 2, which is wrong, In addition, for the step size can not be set to decimals

aRange () function with the above characteristics but its step can be set to a decimal. At the same time it returns a ndarray () so have the RESHAPE () properties

When using the floating point parameters with arange, because limited precision floating point is generally not possible to predict the number of elements obtained. For this reason, it is usually best to use the function linspace, it receives the number of elements we want rather than the step size as a parameter:

np.linspace( 0, 2, 9 )# 9 numbers from 0 to 2

3.python in the dictionary items ()

items function will return a dictionary as a list, because dictionaries are unordered, so the list is the return of disorder. items () method of the dictionary for each key and value to form a tuple, and these tuples in the list returned.

4. dictionary get function

_dict.get (key, value) # if the corresponding key does not exist in the dictionary, the default value for the key corresponding to

5.argsort () function and the sort, sorted create anonymous functions and lambda function usage

The method is applied on the sort of the list, sorted can be sorted objects operate all iterations. list sort method returns a list of the existing operation, the built-in function sorted method returns a new list, rather than operation performed on the basis of the original .

For example: List of the sort just _list.sort ()

Sorted syntax:

sorted(iterable[, cmp[, key[, reverse]]])

Parameter Description:

iterable - iterables.

cmp - compare function, the two parameters, parameter values ​​are taken from the subject may be iterative, this function must comply with the rules is greater than 1 is returned, it is less than -1, is equal to 0 is returned.

key - is mainly used for the comparison element, only one parameter, the specific parameter is a function of iteration may be taken from the object, specify one of the elements in the iteration to be sorted.

reverse - collation, reverse = True descending, reverse = False ascending (default).

Return Value: Returns the reordered list

For example sort the dictionary sorted by

 

import operator
_dict={'a':1,'b':3}
sorted_dict=sorted(_dict.items(),key=operator.itemgetter(1),reverse=True)

 

Return sorted_dict are sorted list [( 'b', 3) , ( 'a', 1)]

or:

_dict = {'a': 1, 'b': 3}
sorted_dict = sorted(_dict.items(), key=lambda x:x[1], reverse=True)

argsort () function

import numpy as np

x=np.array([1,4,3,-1,6,9])

y = x.argsort ()

We found argsort () is a function of the element x in ascending order, which corresponds to the extracted in index (index), and then outputs

print (y), compared with [3,0,2,1,4,5]

6. traversing a plurality of sequences zip () using

Simultaneously traverse two or more sequences, zip () function can be read element pairs.

questions = ['name', 'quest', 'color']
answers = ['lancelot', 'holy', 'blue']
for q, a in zip(questions, answers):
    print(q,a)    

Output:

name lancelot
quest holy
color blue

a_list=['flower','flow','flight']

I want to a_list each element traversal

for i in zip(*a_list):

    print(i)

The output is:

('f', 'f', 'f')

('l', 'l', 'l')

( 'O', 'o', 'i')

('w', 'w', 'g')

Note: In a_list shortest end standard elements, zip outer layer may be applied

 

Guess you like

Origin www.cnblogs.com/dyl222/p/11100158.html