python内置函数

  1. dict()
tuple_data1 = [('one', 1), ('two', 2), ('three', 3)]
tuple_data2 = zip(['one', 'two', 'three'], [1, 2, 3])
dict_data1 = dict(tuple_data1)
dict_data2 = dict(tuple_data2)
dict_data3 = dict(one=1, two=2, three=3)
print(dict_data1)
print(dict_data2)
print(dict_data3)
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
  1. update()
dict1 = {'one':1, 'two':2}
dict2 = {'three':3}
dict1.update(dict2)
print(dict1)
{'one': 1, 'two': 2, 'three': 3}
  1. filter()
list1 = ['', '1', '3', None, []]
list2 = list(filter(None, list1))
print(list2)
['1', '3']
  1. isinstance()
print(isinstance(1,int))       #判断是不是int类型
print(isinstance('abc',str))   #判断字符串
print(isinstance([],list))     #判断列表
print(isinstance({},dict))     #判断字典
print(isinstance([],set))   #判断集合
True
True
True
True
False
  1. zip()
a = [1,2,3]
b = [4,5,6]
zipped = list(zip(a,b))
print(zipped)
[(1, 4), (2, 5), (3, 6)]
  1. Counter
from collections import Counter

def application(x, y):
    new_x, new_y = Counter(x), Counter(y)
    z = dict(new_x + new_y)
    print(z)

if __name__ == "__main__":
    x = {'apple': Decimal(1), 'banana': Decimal(2)}
    y = {'banana': Decimal(10), 'pear': Decimal(11)}
    application(x, y)

结果:

{'apple': Decimal('1'), 'banana': Decimal('12'), 'pear': Decimal('11')}
  1. python比较全的内置函数汇总:内置函数
  2. collections
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值