Common python algorithms and python Algorithms
I:
# Calculate the average, median, and count
#coding:utf-8
# Calculate the average, median, and count
Import random
Lst = [random. randint (0, 10) for I in range (11)]
S = sum (lst)
Length = len (lst)
Average = s * 1.0/length
Print "The average is:", average
Lst. sort ()
Print "From small to bigger,", lst
Middle = lst [length/2]
Print "middle", middle
# Mode method1
M = {}
For I in lst:
If I in m:
M [I] + = 1
Else:
M [I] = 1
Print m
Max_number = max (m. values ())
Print max_number
For k, v in m. items ():
If v = max_number:
Print "mode is:", k
# Mode method2
T = set (lst)
D = {}
For I in t:
D [I] = lst. count (I)
Print d
For k, v in m. items ():
If v = max_number:
Print "mode is:", k
II:
A list consists of several integers. Now you need to put the even number in front and the odd number in the back.
# Coding: UTF-8
# A list consists of several integers. Now you need to put the even number in front and the odd number in the back
Import random
Lst = [random. randint (0,100) for I in range (90)]
Print lst
Odd = []
Even = []
For I in lst:
If I % 2 = 0:
Even. append (I)
Else:
Odd. append (I)
Print even
Print odd
Even. extend (odd)
Print "result:", even
III:
Strings in the list are sorted by specified character order
# Coding: UTF-8
# Sort the strings in the list in the specified character order
By_string = ["a", "e", "I", "o", "u"]
Words = ["suzhou", "shanghai", "hangzhou", "nanjing", "beijing"]
Result = {}
For word in words:
N = []
For I in word:
If I in by_string:
N. append (by_string.index (I ))
Else:
N. append (9)
Result [word] = n
Print result
Vs = result. values ()
Vs. sort ()
Print
Sorted_word = []
For I in:
For k, v in result. items ():
If v = I:
Sorted_word.append (k)
Print sorted_word