码迷,mamicode.com
首页 > 其他好文 > 详细

内置方法

时间:2018-09-16 22:30:20      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:[1]   nbsp   one   lambda   array   oca   lam   bcd   方法   

print( all([1,-5,3]) )
print( any([]) )
a= ascii([1,2,"开外挂开外挂"])
print(type(a),[a])
a = bytes("abcde",encoding="utf-8")
b = bytearray("abcde",encoding="utf-8")
print( b[1] )
b[1]= 50
print(b)

结果:

True
False
<class ‘str‘> ["[1, 2, ‘\\u5f00\\u5916\\u6302\\u5f00\\u5916\\u6302‘]"]
98
bytearray(b‘a2cde‘)

 

 

def sayhi():pass
print( callable(sayhi) )
结果:
True


def sayhi(n):
print(n)
for i in range(n):
print(i)
sayhi(3)

# (lambda n:print(n))(5)
calc = lambda n:3 if n<4 else n
print(calc(2))
结果:

3
0
1
2
3

 

 

 

def test():
local_var =333
print(locals())
# print(globals())
test()
# print(globals())
print(globals().get(‘local_var‘))
结果:

{‘local_var‘: 333}
None

 

 

分别按key和value排序
a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(a )
print( sorted(a.items()) )
print( sorted(a.items(),key=lambda x:x[1]) )
结果:

{6: 2, 8: 0, 1: 4, -5: 6, 99: 11, 4: 22}
[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]



a = [1,2,3,4,5,6]
b = [‘a‘,‘b‘,‘c‘,‘d‘]

for i in zip(a,b):
print(i)
结果:

(1, ‘a‘)
(2, ‘b‘)
(3, ‘c‘)
(4, ‘d‘)

 

内置方法

标签:[1]   nbsp   one   lambda   array   oca   lam   bcd   方法   

原文地址:https://www.cnblogs.com/tengtianshan/p/9657707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!