Topic 1 Python realize rounding the number of float
DEF panF (F): IF the isinstance (F, float): # Number of determining whether the input to float isinstance (object, classinfo) determines whether the instance of this class or object is variable b = int (str (f) .split ( ' . ' ) [0]) # the number converted to a string and then divided, with '.' dividing the first half decimal point to obtain C = STR (F) .split ( ' . ' ) [. 1] # post-division the second half of the decimal point iF int (C [0:. 1])> = 5: # the second half of the divided data after determining whether the first number is greater than 5, before the comparison is first converted to type int B +. 1 = # decimal half The first part of the number will be greater than the integer part plus 5. 1 the else : return B # otherwise directly returns the integer portion of the returnB the else : return (U ' is not a float type parameter ' ) Print (panF (4.5999))
Title 2 Python dictionary sorted according to the key
= {DIC ' name ' : ' Laohu ' , ' Age ' : ' Twenty ' , ' height ' : ' 178cm ' } RES = the sorted (dic.items (), Key = the lambda X: X [0], Reverse = True ) Print ( " in accordance with the key in descending order: " , RES) Result = the sorted (dic.items (), key = the lambda X: X [. 1], Reverse = False) Print ( " according to the ascending order value " , Result) #key = lambda x: x [0 ], reverse = True) # 0 in accordance with the first element in descending order # dic.items () object to be sorted; # Key X = the lambda: X [0] for the foregoing object data values of the first dimension (i.e., key) to sort. # Key = the lambda variable: variable [dimension]. Dimension can be set according to their needs. # Reverse = True elemental descending order key = lambda x: x [0 ] 0-th element value is sorted key # Reverse Ascending = TFalse
Problem 3: Given a non-null positive integer array, the number of values occur in the presence of a new dictionary, and sorted by the number of occurrences of
DEF dict_sort (array_L): DIC = {} for I in array_L: # sequentially given iteration to the element array IF array_L.count (I)> 0: # times list.count () statistics appear in the list of elements DIC [ I] = array_L.count (I) # dictionary storage element and the number of their occurrence, (key, value) = (element i, the number of COUNT (I)) # of elements in dic sorted in descending order of the number of how many = the sorted newdic (dic.items (), Key = the lambda items: items [. 1], Reverse = True) return newdic # return the result in descending order array_L = [. 1,. 1,. 1, 6,6,. 6,. 7, . 7,. 9,. 3 ] RES = dict_sort (array_L) Print ( " result: " , RES)
Remarks:
The python python list is built-in data types, data class list are not necessarily the same, but the type of the array must all be the same.
Saved data types in the list is stored in the address data, a pointer is simply not data, such a list stored too much trouble, for example, list1 = [1,2,3, 'a' ] needs 4 and four data pointers, and the storage consumption increases cpu.
encapsulated numpy array has a very powerful feature, which is the same type of data stored
Question 4: find an array of integers and the sum of the two equals a target value, and the presence of two subscripts returned list
# Given an array of integers and a target, find two numbers in the array and to the target value.
# You can assume only one answer corresponding to each input, and the same element can not be reused.
Example #: = Given the nums [2,. 7,. 11, 15], target =. 9
# because nums [0] + nums [1 ] = 2 + 7 = 9 is returned [0, 1]
class Solution (): DEF GetSum (Self, the nums, target): nums_index = [] # for storing target data acquired is stored in the index list for I in Range (len (the nums)): # Get the array length , each element in turn iteration cycles for J in Range (I +. 1, len (the nums)): # cycle, the current element and each element thereof is sequentially added after the target value and comparing the same whether the iF the nums [I] + the nums [J] == target: nums_index.append (I) # condition is satisfied, an additional element subscript nums_index.append (J) return nums_index # return results IF the __name__ == " __main__": nums=[2,7,8,9,11,4] target=15 l=Solution() res=l.getSum(nums,target) print("结果:",res)