Book菜鸟Python 上手指南

Python 强化训练:第一篇

2016-10-30  本文已影响465人  谢小路

强化训练: 第一篇

目标

0.

校招算是结束了吧!
简单回顾几句:

  1. 校招python岗位极少,多是初创型公司对python需求大,与之相对应的是要求偏高,大多要求上班就上手工作的这种,同时希望实习的时间充裕。
  2. 数据结构和算法都是必备。
  3. 至少还应该掌握c++, java中的一种:java岗更多,c++水太深。
  4. 数据库至少掌握MySQL。
  5. 基础越扎实越有利。
  6. 互联网行业竞争太激烈。
  7. 实习经验越丰富越好,前提是你的实习和所求职岗位契合度高。

1.

获得岗位:后端开发工程师,使用语言:python , C++

  1. 强化练习python
  2. 完善编码风格
  3. 学艺不精难成事
  4. 输出:强调输出

内容

  1. 赋值
  2. 查找最大或者最小的N个元素
  3. 命名切片
  4. 列表,集合,字典解析式:过滤
  5. 元组命名
  6. 统计词频及出现次数最多 的几个数
  7. 字典排序
  8. 字典公共键
  9. 字典有序
  10. 队列

1. 赋值

NAME, AGE, EMAIL, UNIVERSITY = range(4)
print(NAME, AGE, EMAIL, UNIVERSITY) #0 1 2 3
ONE, TWO, *THREE, FOUR = range(10)
print(ONE, TWO, THREE, FOUR)#0 1 [2, 3, 4, 5, 6, 7, 8] 9
data = ["one", "two", "three", "four", "five"]
One_one, Two_two, _, _, Five_five = data
print(One_one, Two_two, Five_five)#one two five

2. 查找最大或者最小的N个元素

from random import randint
import heapq
number = [randint(-100, 10000) for _ in range(100)]
print(heapq.nlargest(3, number)) # [9906, 9901, 9773]
print(heapq.nsmallest(3, number)) # [79, 233, 251]

all_data =[ {'name': 'IBM', 'shares': 100, 'price': 91.1}, 
{'name': 'AAPL', 'shares': 50, 'price': 543.22}, 
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75}, 
{'name': 'YHOO', 'shares': 45, 'price': 16.35}, 
{'name': 'ACME', 'shares': 75, 'price': 115.65}]

cheap = heapq.nsmallest(2, all_data, key=lambda a: a["price"]+a["shares"])

expensive = heapq.nlargest(2, all_data, key=lambda a: a["price"]+a["shares"])

print(cheap, "\n", expensive)
# [{'price': 16.35, 'name': 'YHOO', 'shares': 45}, {'price': 31.75, 'name': 'HPQ', 'shares': 35}]

# [{'price': 543.22, 'name': 'AAPL', 'shares': 50}, {'price': 21.09, 'name': 'FB', 'shares': 200}]

3. 命名切片

from random import randint
score = [randint(50, 101) for _ in range(70)]
first_ten = slice(0, 10)
second_five = slice(10, 15)
last_three = slice(67, 70)
print(score)
print(score[first_ten], first_ten.start, first_ten.stop)# [54, 63, 91, 53, 95, 67, 76, 94, 58, 89] 0 10
print(score[second_five], second_five.start, second_five.stop,second_five.step)# [72, 72, 81, 86, 100] 10 15 None
print(score[last_three], last_three.start, last_three.stop, last_three.step)# [58, 90, 90] 67 70 None

4. 列表,集合,字典解析式:过滤

from random import sample
## 过滤奇偶数
list_number = [randint(-10, 10) for _ in range(15)]
ist_even = [i for i in list_number if i % 2 == 0]
list_odd = [j for j in list_number if j % 2 == 1]
print(list_number)
print(list_even)
print(list_odd)
# [-8, 5, 4, -1, 5, -1, 6, 6, 2, 6, 2, -7, -4, -9, -10]
# [-8, 4, 6, 6, 2, 6, 2, -4, -10]
# [5, -1, 5, -1, -7, -9]
## 根据字典值过滤奇偶数
dict_number = {k: randint(-10, 10) for k in sample("abcdefghijkmnvc", 7)}
dict_even = {i: j for i, j in dict_number.items() if j % 2 == 0}
dict_odd = {i:j for i, j in dict_number.items() if j %2 ==1}
print(dict_number)
print(dict_even)
print(dict_odd)
# {'e': 5, 'g': 7, 'j': 5, 'd': 5, 'v': 10, 'k': -6, 'n': 3}
# {'v': 10, 'k': -6}
# {'e': 5, 'g': 7, 'd': 5, 'n': 3, 'j': 5}

## 过滤集合
set_data = set([randint(-10,10) for _ in range(10)])
set_even = {i for i in set_data if i % 2 == 0}
set_odd = {j for j in set_data if j % 2 == 1}
print(set_data)
print(set_even)
print(set_odd)
# {0, 8, 9, 10, -8, -6, -5}
# {0, 8, 10, -6, -8}
# {9, -5}

5. 元组命名

from collections import namedtuple
tuple_student = ("xiewei", 24, "xie_wei_sh@foxmail.com", "ShanghaiUniversity")
NAME, AGE, EMAIL, UNIVERSITY = range(4)
print(tuple_student[NAME], tuple_student[EMAIL])

student1 = namedtuple("student", ["NAME", "AGE", "EMAIL", "UNIVERSITY"])
s = student1("xiewei", 24, "xie_wei_sh@foxmail.com", "ShanghaiUniversity")

print(s.NAME, s.AGE, s.EMAIL, s.UNIVERSITY)

6. 及出现次数最多 的几个数

from random import randint
data = [randint(0, 20) for _ in range(30)]
c = dict.fromkeys(data, 0)
for i in data: c[i] += 1

from collections import Counter
d = Counter(data)
e = d.most_common(3)
print(c, "\n", d, "\n", e)

7. 字典排序

dict_data = {x: randint(60, 100) for x in 'abcdefg'}
new_dict_data = sorted(dict_data)

## zip
new_dict_data_1 = sorted(zip(dict_data.values(), dict_data.keys()))

new_dict_data_2 = sorted(dict_data.items(), key=lambda x: x[1])

print(dict_data, "\n", new_dict_data, "\n", new_dict_data_1, "\n", new_dict_data_2)

8. 字典公共键

from random import randint, sample
s1 = sample("abcdef", randint(3, 5))
s2 = sample("abcdef", randint(3, 5))
s3 = sample("abcdef", randint(3, 5))
print(s1, s2, s3)
dict_1 = {x: randint(1, 4) for x in s1}
dict_2 = {x: randint(1, 4) for x in s2}
dict_3 = {x: randint(1, 4) for x in s3}
print(dict_1, dict_2, dict_3)
## 集合与操作
common_key = dict_1.keys() & dict_2.keys() & dict_3.keys()
print(common_key)
all_keys = map(dict.keys, [dict_1, dict_2, dict_3])
print(list(all_keys))
## 集合,map , reduce :python3中 from functools import reducefrom functools import reduce
common_key_2 = reduce(lambda a, b: a & b, map(dict.keys, [dict_1, dict_2, dict_3]))

print(common_key_2)

9. 字典有序

from collections import OrderedDict
new_order_dict = OrderedDict()
new_order_dict["one"] = 1
new_order_dict["two"] = 2
new_order_dict["three"] = 3
new_order_dict["four"] = 4
new_order_dict["five"] = 5
print(new_order_dict)

10. 队列

from collections import deque
queue = deque([], maxlen=3)
queue.append(1)
queue.append(2)
queue.append(3)
queue.append(4)
print(queue)
# 求一个固定滑动窗口大小为n的平均值
""" 
h.avg(2) = 2/1 假设n = 3 
h.avg(3) = (2+3)/2 
h.avg(4) = (2+3+4)/3 
h.avg(5) = (3+4+5)/3
"""
class h(object):    
      def __init__(self, size):        
          self.n = size        
          self.queue = deque([], maxlen=size)    
    def avg(self, number):        
          self.queue.append(number)        
          print(self.queue, len(self.queue))        
          if len(queue) < self.n:            
                  return sum(self.queue)/(len(self.queue))        
          else:            
                  return sum(self.queue)/self.n
A = h(4)
B = A.avg(2)
print(B)
C = A.avg(3)
print(C)
D = A.avg(4)
print(D)
E = A.avg(5)
print(E)
F = A.avg(6)
print(F)

上一篇下一篇

猜你喜欢

热点阅读