内置方法
Posted tengtianshan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内置方法相关的知识,希望对你有一定的参考价值。
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‘)
以上是关于内置方法的主要内容,如果未能解决你的问题,请参考以下文章