python基础-内置函数(2)

一、创建集合、字典、列表、元组的函数

  1、创建集合:set()

s=set()   #生成一个空集合
s1=set([11,22,33,44,11])   #生成一个集合

   2、创建字典:dict()

a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})

  3、创建列表:list()

a=list([11,22,33,44])

  4、创建元组: tuple()

a = tuple([1,3,4,5,5])

 二、筛选:filter(函数,可迭代的对象)

def f1(a):
    if a>22:
        return True

li=[11,22,33,44,55,66]
ret=filter(f1,li)
print(list(ret))       #筛选出大于22的列表元素

三、map函数:map(函数,可迭代的对象)

li=[11,22,33,44,55,66]
ret=map(lambda a:a+200,li)

结果:[211, 222, 233, 244, 255, 266]

 filter和map的区别在于:filter的参数中,函数返加True,则把可迭代对象中的元素添加到结果中,而map的参数中,是直接把函数的返回值添加到结果中

四、zip函数

l1=['Welcome',11,33,44]
l2=['to',11,33,44]
l3=['Beijin',11,33,44]
r=zip(l1,l2,l3)
temp=list(r)[0]
ret=' '.join(temp)
print(ret)

结果:Welcome to Beijin

 

posted @ 2017-11-12 13:03  囚徒健身  阅读(164)  评论(0编辑  收藏  举报