Essential notes0.py - #1.1-collections 001 \u8fd4\u56de LIST \u91cc\u7684\u6700\u5927\u6700\u5c0f\u503c minmax[8 7 2 5 1 =(1 8 def minmax(L assert hasattr(L_iter YOUR CODE HERE

Essential notes0.py - #1.1-collections 001 u8fd4u56de...

This preview shows page 1 - 2 out of 2 pages.

#1.1-collections """ 001 返回 LIST 里的最大最小值 """ minmax([8, 7, 2, 5, 1]) == (1, 8) def minmax(L): assert hasattr(L, "__iter__") # # YOUR CODE HERE # xmax, xmin = max(L), min(L) return xmin, xmax """ 002 移除 LIST 里的某个值 """ remove_all([1, 2, 3, 2, 4, 8, 2], 2) == [1, 3, 4, 8] def remove_all(L, x): assert type(L) is list and x is not None # # YOUR CODE HERE L = [i for i in L if i != x] return L """ 003 LIST 返回一个字典 DICT KEY 为非 0 数的 INDEX VALUE 为非 0 数的值 """ x = [0.0, 0.87, 0.0, 0.0, 0.0, 0.32, 0.46, 0.0, 0.0, 0.10, 0.0, 0.0] d['inds'] = [1, 5, 6, 9] d['vals'] = [0.87, 0.32, 0.46, 0.10] def compress_vector(x): assert type(x) is list d = {'inds': [], 'vals': []} # # YOUR CODE HERE for i, j in enumerate(x): if j != 0: d['inds'].append(i) d['vals'].append(j) return d """ 004 将字典转换成 LIST """ d = {} d['inds'] = [0, 3, 7, 3, 3, 5, 1] d['vals'] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0] x_true = [1.0, 7.0, 0.0, 11.0, 0.0, 6.0, 0.0, 3.0] def decompress_vector(d, n=None): # Checks the input assert type(d) is dict and 'inds' in d and 'vals' in d, "Not a dictionary or
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

  • Fall '19
  • susan lee

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes