# 如何进行反向迭代以及如何实现反向迭代
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 FloatRange:
de...
# 使用生成器函数实现可迭代对象
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()
# print(g.__next_...
# 实现可迭代对象和迭代器对象
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 = requests.get(u'http:...
# 用户历史记录功能
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 collections i...
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 = {x: randint(1, 4)...
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.items()
print(d3)
...
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(c)
for x in data...
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)
print(student[NAME...
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)
print(res)
# 生成...