Python使用生成器函数实现可迭代对象

Jackey Python 1,681 次浏览 0 评论
# 使用生成器函数实现可迭代对象 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_...

Python实现可迭代对象和迭代器对象

Jackey Python 1,704 次浏览 0 评论 ,
# 实现可迭代对象和迭代器对象 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:...

Python实现用户的历史记录功能

Jackey Python 1,729 次浏览 0 评论
# 用户历史记录功能 from random import randint from collections import deque import pickle from pip._vendor.distlib.compat import raw_input # 猜数字小游戏 N = randint(0, 100) # 创建5个元素的队列,保留最近五...

Python让字典保持有序

Jackey Python 1,670 次浏览 0 评论 ,
# 让字典保持有序 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...

Python快速查找多个字典中的公共键(key)

Jackey Python 1,746 次浏览 0 评论 ,
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)...

Python根据字典中的值大小排序

Jackey Python 2,800 次浏览 0 评论 , ,
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) ...

Python统计序列中元素出现的频率

Jackey Python 2,871 次浏览 0 评论
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...

Python为元组中的每个元素命名

Jackey Python 2,830 次浏览 0 评论 ,
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...
Go