首页 > 技术文章 > locals()max()min()next()20181011

wujianghu 2018-10-11 15:08 原文


def locals(*args , **kwargs):#real signature unknown
	'''
	Return a dictionary containing the current scope's【范围】 local variables.【当间作用域的局部变量】.

	NOTE:Whether or not updates to this dictionary will affect name lookups【lookup查找】in the local scope and vice-versa【反之亦然】is *implementation dependent* and not covered by and backwards compatibility guarantees.
	'''
	pass

	remark = '''
	locals()查找局部变量名及变量,返回一个字典,在globals()已举例

	'''

	baidu_translate ='''
	
	Return a dictionary containing the current scope's local variables.

	返回包含当前作用域的局部变量的字典。



	NOTE: Whether or not updates to this dictionary will affect name lookups in

	注意:是否更新本词典会影响名称查找

	the local scope and vice-versa is *implementation dependent* and not

	局部范围和反之亦然是*实现依赖*而不是

	covered by any backwards compatibility guarantees

	任何向后兼容性保证'''


	baidu_words = '''
	dictionary词典,字典; 代码字典
	containing包含; 控制( contain的现在分词 ); 克制; 牵制
	scope范围; 眼界,见识; 余地; 广袤,地域; 审视,仔细研究
	local variables区域变数.   :局部变量
	Whether or not是否; 无论
	vice-versa反之亦然
	dependent依赖的; 依靠的; 取决于…的; 有瘾的; 受抚养人; 受赡养者; 依赖他人者; 扈从,侍从
	backwards向后的; 倒行的; 返回的; 落后的; 向后; 倒; 往后; 逆
	compatibility适合; 互换性; 通用性; 和睦相处
	guarantees担保( guarantee的名词复数 ); 担保品; 保修单; 抵押品
		'''

def max(*args , **kwargs):#known special case of max【已知的特定实例最大值】
	'''
	max(iterable , *[,default=obj,key=func]) ---> value
	max(arg1,arg2,*args,*[,key=func]) ---> value

	With a single iterable argument ,return its biggest item.The default keyword-only argument specifes and object to return if the provided iterable is empty.
	With two or more arguments,return the largest argument.
	'''
	pass

	remark = '''
	求最大值
	max(arg1, arg2, *args, *[, key=func])这个形式的含义为:
		arg1,arg2是必传的,也就是至少两个参数
		*args代表可以传入多个参数,以元祖的形式传入
		*[,key=func]。把[]去掉就是,key=func。[]里面可有可无
		举例:
			max(arg1,arg2,arg3 , key=func) ---> 本质:max(arg1,arg2,(arg3,) , key=func):arg3后面的逗号代表是元祖
	'''



	baidu_translate = '''
	max(iterable, *[, default=obj, key=func]) -> value

		max(迭代,*[默认= Obj',key=Func])->值

	max(arg1, arg2, *args, *[, key=func]) -> value

		MAX(ARG1,ARG2,*ARGS,*[,密钥= FUNC])->值

	With a single iterable argument, return its biggest item. The

		使用一个可重复的参数,返回其最大项。这个

	default keyword-only argument specifies an object to return if

		默认关键字仅参数指定要返回的对象

	the provided iterable is empty.

		所提供的迭代是空的。

	With two or more arguments, return the largest argument.

		使用两个或更多个参数,返回最大的参数。
	'''


	baidu_words = '''
	default未履行任务或责任; 受传唤时未出庭; 由于不到庭而败诉; 弃权; 未履行,拖欠; 未参加或完成; 因未到庭而败; 未到庭; 
			缺省,默认
	obj目标; object 物体; 项目; objection 反对
	func功能; 作用; function 官能; 职责
	argsanti-radar guidance sensor 防雷达跟踪传感器
	argument论据; 争论,争吵; 幅角; 主题,情节
	biggest大的,长大的; 大的( big的最高级 ); 重要的; 庞大的; 大方的
	specifies指定( specify的第三人称单数 ); 详述; 提出…的条件; 使具有特性
	object to反对, 对…反感; 非; 不以为然
	empty空的,空虚的,空洞的; 空闲的,无效的,徒劳的; 无聊的,愚蠢的; 言语或行动空洞的; 成为空的, 把…弄空; 把…腾出来; 成为空的; 流空; 空车; 空的东西
	arguments论据; 争吵; 争论( argument的名词复数 ); 说理

	'''

	#代码,验证
	'和下面的min()一起'









求最小值函数min()--->https://www.cnblogs.com/whatisfantasy/p/6273913.html

def min(*args , key=None):#known special case of min
	'''
	min(iterable , *[,default=obj,key=func]) ---> value
	min(arg1 , arg2 , *args,*[,key=func]) ---> value

	With a single iterable argument ,return its smallest item.The default keyword-only argument specifies an object to return if the provided iterable is empty.
	With two or more arguments,return the smallest argument.
	'''
	pass
	remark = '''
	求最小值,当提供的可迭代的对象为空时,返回一个指定的对象
	'''



	baidu_translate = '''
	min(iterable, *[, default=obj, key=func]) -> value

		min(迭代,*[缺省值= OBJ,KEY=FUNC])->值

	min(arg1, arg2, *args, *[, key=func]) -> value

		min(ARG1,ARG2,*ARGS,*[,密钥= FUNC])->值


	With a single iterable argument, return its smallest item. The

		使用一个可重复的参数,返回其最小项。这个

	default keyword-only argument specifies an object to return if

		默认关键字仅参数指定要返回的对象

	the provided iterable is empty.

		所提供的迭代是空的。

	With two or more arguments, return the smallest argument.

		使用两个或多个参数,返回最小的参数。
	'''


	baidu_words = '''
	minminute 分; minuto (Portuguese or Spanish=minute) 分; minority 少数; miniature 小型的
	default未履行任务或责任; 受传唤时未出庭; 由于不到庭而败诉; 弃权; 未履行,拖欠; 未参加或完成; 因未到庭而败; 未到庭; 缺省,默认
	obj目标; object 物体; 项目; objection 反对
	func功能; 作用; function 官能; 职责
	argsanti-radar guidance sensor 防雷达跟踪传感器
	argument论据; 争论,争吵; 幅角; 主题,情节
	smallest小的( small的最高级 ); 小气的; 小号的; 小规模的
	specifies指定( specify的第三人称单数 ); 详述; 提出…的条件; 使具有特性
	object to反对, 对…反感; 非; 不以为然
	arguments论据; 争吵; 争论( argument的名词复数 ); 说理
	'''

	#代码,验证

>>> products = {"买房1":350,"买车2":20,"娶老婆":50,"生孩子":30,"小孩教育":10}
>>> max(products)#直接传字典,集合默认用key排序的
'生孩子'

>>> key = list(products.keys())
>>> key
['买房1', '买车2', '娶老婆', '生孩子', '小孩教育']
>>> max(key)
'生孩子'


>>> min(products)
'买房1'
>>> min(key)
'买房1'


>>> max(products, key=lambda x:products[x])#指定按value排序,products[key] = value
'买房1'#打印最大值项目的key

>>> value = list(  products.values()  )
>>> value
[350, 20, 50, 30, 10]
>>> max(value)
350#打印最大值项目的value
#怎么样把最大值,最小值的key,value都弄出来?  zip()
#参数为字典|集合时 按key排序


>>> min(products, key=lambda x:products[x])
'小孩教育'


>>> max(zip(products.keys() , products.values() ))
('生孩子', 30)
>>> max(zip(products.values() , products.keys() ))#把products的key,value,zip成了value,key
(350, '买房1')
>>> min(zip(products.values() , products.keys() ))
(10, '小孩教育')
>>> min_zip_turn = min(zip(products.values() , products.keys() ))
>>> min_zip_turn
(10, '小孩教育')
>>> goal = {min_zip_turn[1]:min_zip_turn[0]}
>>> goal
{'小孩教育': 10}

#下面是想看看zip()
>>> z = zip(  products.values() , products.keys()  )
>>> z
<zip object at 0x1021e7348>
>>> list(z)
[(350, '买房1'), (20, '买车2'), (50, '娶老婆'), (30, '生孩子'), (10, '小孩教育')]
>>> tuple(z)#这下成空的了,关于zip的知识点,后面再深究
()
>>> z
<zip object at 0x1021e7348>
>>> list(z)
[]
>>> z = zip(  products.values() , products.keys()  )#重新zip一下
>>> tuple(z)
((350, '买房1'), (20, '买车2'), (50, '娶老婆'), (30, '生孩子'), (10, '小孩教育'))
>>> z = zip(  products.values() , products.keys()  )
>>> set(z)
{(350, '买房1'), (20, '买车2'), (50, '娶老婆'), (30, '生孩子'), (10, '小孩教育')}
>>> z = zip(  products.values() , products.keys()  )
>>> dict(z)
{350: '买房1', 20: '买车2', 50: '娶老婆', 30: '生孩子', 10: '小孩教育'}
>>> max( dict(zip(  products.values() , products.keys()  )) )
350
>>> max(zip(  products.values() , products.keys()  ))
(350, '买房1')
>>> 

#排大小,max,min会有自己的规则
>>> max(chr(65) , chr(66) , chr(100))
'd'#chr(100)
>>> chr(65)
'A'
>>> chr(66)
'B'
>>> chr(100)
'd'
>>> max("小米1","小米5","小米8")#8在ASCII码里比1,5后。所以大
'小米8'
>>> max("小米1","小米11","小米111")#
'小米111'
>>> max("小米1000","小米11","小米111")
'小米111'
>>> max("爱小米1","小米11","小米111")#这是什么规律呢
'爱小米1'
>>> 









next()生成迭代器里下一个元素

def next(iterator , default=None):#real signature unknown;restored from __doc__
	'''
	next(iterator[,default])
	
	Return the next item from the iterator.
	If default is given and the iterator is exhausted【用尽的】,it is returned instead of raising StopIteration.
	'''
	pass

	remark = '''
	从迭代器获取并返回下一个项目,如果项目用完了就抛出StopIteration异常,for 循环不会抛出。
	如果给了默认值,并且默认值在迭代器里会把默认值直接抛出,就算迭代器就算用完了,也不会抛出异常(详见验证)。
	格式:next(iterator[,default])。比如next(iter([1,2,3]) ,2).不是next(iter([1,2,3]) ,default=2)
	'''

	baidu_translate_set = '''
	next(iterator[, default])

		下一步(迭代器[默认])

	Return the next item from the iterator.

		从迭代器返回下一个项目。

	If default is given and the iterator is exhausted,

		如果给定默认值以及迭代器已用完,

	it is returned instead of raising StopIteration.

		它返回这个默认值,而不是抛出异常说:停止迭代。
	'''

	baidu_words = '''
	next紧接在后的; 次于的; 贴近的; 紧邻的; 接下去; 然后; 居后地; 依次的; 下一个; 下一位; 靠近; 居于…之后; 在…的隔壁
	iterator迭代器,迭代程序
	default未履行任务或责任; 受传唤时未出庭; 由于不到庭而败诉; 弃权; 未履行,拖欠; 未参加或完成; 因未到庭而败; 未到庭; 
			缺省,默认
	item项目; 条,条款; 一则; 一件商品; 又,同上
	given指定的,确定的; 假设的,假定的; 有…倾向的; 赠送的; 考虑到; 倘若,假定; 假设; 给予,赠送( give的过去分词)
	exhausted精疲力竭的; 筋疲力尽的; 耗尽的; 用完的; 用尽( exhaust的过去式和过去分词 ); 使非常疲倦; 详尽讨论; 弄空
	returned被送回的,归来的; 恢复; 归还; 返回,回来( return的过去式和过去分词 ); 还
	instead of代替…, 而不是…, 而不用…
	raising提高; 增加; 高岗; 高地; 养育; 引起( raise的现在分词 ); 提起; 唤起

	'''



	#代码验证
>>> iter_l = iter([1,2,3])
>>> next(iter_l)
1
>>> next(iter_l)
2
>>> next(iter_l)
3
>>> next(iter_l)#next()取完了就报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

>>> next(iter_l , default=2)#不是这种语法形式
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: next() takes no keyword arguments

>>> next(iter_l , 2)
2
>>> next(iter_l , 2)
2

>>> next(iter_l , 3)
3
>>> next(iter_l , 4)#没有4也返回4 , 并且类型是int。这样目前觉得没有意义,不知道设计者的角度是什么
4
>>> next(iter_l , 5)
5
>>> a = next(iter_l , 5)
>>> a
5
>>> type(a)
<class 'int'>
>>> 



推荐阅读