# 如何在一个for语句中迭代多个可迭代对象
# 并行
from random import randint
from mrjob.py2 import xrange
chinese = [randint(60, 100) for _ in xrange(40)]
math = [randint(60, 100) for _ in ...
# 如何对迭代器做切片操作
f = open('test.log')
# f.readlines() 一次性把文件读入到内存当中
# lines = list(f.readlines())
# print(lines[100:300])
# for line in f:
# print(line)
from i...
# 如何进行反向迭代以及如何实现反向迭代
l = [1, 2, 3, 4, 5]
# 这种方式改变了原列表
# l.reverse()
# print(l)
lNew = l[::-1]
print(lNew)
print(l)
for x in reversed(l):
print(x)
class...
# 使用生成器函数实现可迭代对象
from mrjob.py2 import xrange
def f():
print('in f(), 1')
yield 1
print('in f(), 2')
yield 2
print('in f(), 3')
yield 3
g = f(...
# 实现可迭代对象和迭代器对象
l = [1, 2, 3, 4]
s = 'abcde'
for x in l: print(x)
for x in s: print(x)
print(iter(l))
print(iter(s))
import requests
def getWeather(city):
r = ...
# 用户历史记录功能
from random import randint
from collections import deque
import pickle
from pip._vendor.distlib.compat import raw_input
# 猜数字小游戏
N = randint(0, 100)
# 创建5个元...
# 让字典保持有序
from mrjob.py2 import xrange
from pip._vendor.distlib.compat import raw_input
d = {}
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)
for k in d:
print(k)
...
from functools import reduce
from random import randint, sample
# 获取多个字典中相同的key
data1 = sample('abcdefg', 3)
print(data1)
data2 = sample('abcdefg', randint(3, 6))
print(data2)
s1...
from random import randint
d = {x: randint(60, 100) for x in 'xyzabc'}
print(d)
# 将字典转化为元组, 第一种方式
d2 = list(zip(d.values(), d.keys()))
print(sorted(d2))
# 第二种方式
d3 = d....
from random import randint
from mrjob.py2 import xrange
data = [randint(0, 20) for _ in xrange(30)]
print(data)
# 统计每个数值出现的频率,第一种方式
c = dict.fromkeys(data, 0)
print...
from mrjob.py2 import xrange
student = ('Jim', 16, 'male', 'jim8721@gmail.com')
print(student[0])
# NAME = 0
# AGE = 1
# GENDER = 2
# EMAIL = 3
NAME, AGE, GENDER, EMAIL = xrange(4)
...
from random import randint
from mrjob.py2 import xrange
# 列表筛选大于0的数
# 常用写法
data = [1, 5, -3, -2, 6, 0, 9]
res = []
for x in data:
if x >= 0:
res.append(x)
...
设计模式的六大原则
开闭原则
一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。在软件的生命周期内,因变化、升级和维护对原有代码进行修改,可能会引入错误。所以当软件需要变化时,通过扩展软件实...
创建文件Base.go
package visitor
type Customer interface { // 接待者
Accept(Visitor)
}
type Visitor interface { // 访问者
Visit(Customer)
}
创建文件EnterpriseCustomer.go
packa...
创建文件Command.go
package command
type Command interface {
Execute() // 执行
}
创建文件motherBoard.go
package command
import "fmt"
type MotherBoard struct {
}
func (*Mother...