List python list of averaging and median examples

This article describes the python list List of averaging and median instance, has a good reference value, we want to help. Xiao Bian together to follow up to see it
I ado, directly on the code!

import numpy as np
a = [2,4,6,8,10]
average_a = np.mean(a)
median_a = np.median(a)

Knowledge supplement: python- find two lists median

Description Title:
given size m and n are two ordered arrays and nums1 nums2.

Please find both the median and orderly array, and requires time complexity of the algorithm is O (log (m + n)).

You can not assume nums1 and nums2 both empty.

Example. 1:
nums1 = [. 1,. 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3) / 2 = 2.5

# nums1 = [1,3,3]
# nums2 = [2,5,4]
def list_model(nums1,nums2):
  nums = nums1 + nums2
  nums.sort()
  print("您输入的两列表为 :",nums)
  n = len(nums)
 
  # print((nums[int(n/2-1)] + nums[int(n/2)])/2)
#   print(n)
  if n%2==0:
    model = (nums[int(n/2-1)] + nums[int(n/2)])/2
  else:
    model = nums[int((n+1)/2 - 1)]
  return(model)
 
   
 
x = input("请输入第一个列表 :")
nums1 = x.split(',')
nums1 = [float(nums1[i]) for i in range(len(nums1))]
 
y = input("请输入第二个列表 :")
nums2 = y.split(',')
nums2 = [float(nums2[i]) for i in range(len(nums2))]
 
print("您所求的两列表的中位数为 :",list_model(nums1,nums2))
请输入第一个列表 :1,2.5,4
请输入第二个列表 :2,3,4
您输入的两列表为 : [1.0, 2.0, 2.5, 3.0, 4.0, 4.0]
您所求的两列表的中位数为 : 2.75

List of python list above this demand mean and median example is the small series to share the entire contents of all of the
write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer Share before learning experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based information project, a day to you on the latest technology python, prospects, learning small details that need to comment

Published 22 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun08/article/details/104741115