Python一些常用方法

Python一些常用方法

(仅作为个人笔记,如有雷同,请联系删除。。)

1、常用方法:

求阶乘:reduce(lambda x,y:x*y, range(1, 101))

求和:sum(range(101))

合并字典:dict1.update(dict2)

去重:[x for x in set(listx)]

排序:listx.sort() | sorted(listx)

统计:collections.Counter(listx) | listx.count(argv)

zip(list1, list2, ......)

enumerate(listx)

dictx.items()、dictx.keys()、dictx.values()

format传参:
str = "helle, {1}, let's to {2}"``.format``(parm1, parm2)
str = f"helle, {parm1}, let's to {parm2}"

1+1、常用内置函数:

abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()

2、三个内建函数:

  1. map(func, listx):listx中的值依次传入func,返回一个新的序列。eg:list(map(lambda x:x*x, range(1,10))

  2. filter(func, listx):listx中的值依次传入func,留下返回true的,构成一个新的序列。eg:list(filter(lambda x:x%2==0, range(100)))

  3. reduce(func, listx):func接收两个参数,listx中的值依次传入func,会对func的结果进行累积,作为下一次调用的一个参数,一般会以listx中的前两个值作为初始参数调用func,返回一个最终值.
    eg:reduce(lambda x,y:x*y, range(1, n+1))、 reduce(lambda x,y:x+y, range(10))

    注:python3已不支持,需要from functools import reduce

3、左移、右移:

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值