- 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}
- update()
dict1 = {'one':1, 'two':2}
dict2 = {'three':3}
dict1.update(dict2)
print(dict1)
{'one': 1, 'two': 2, 'three': 3}
- filter()
list1 = ['', '1', '3', None, []]
list2 = list(filter(None, list1))
print(list2)
['1', '3']
- isinstance()
print(isinstance(1,int)) #判断是不是int类型
print(isinstance('abc',str)) #判断字符串
print(isinstance([],list)) #判断列表
print(isinstance({},dict)) #判断字典
print(isinstance([],set)) #判断集合
True
True
True
True
False
- zip()
a = [1,2,3]
b = [4,5,6]
zipped = list(zip(a,b))
print(zipped)
[(1, 4), (2, 5), (3, 6)]
- 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')}
- python比较全的内置函数汇总:内置函数
- collections