Common python algorithms and python Algorithms

Source: Internet
Author: User

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



Related Article

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.