day7: python functions written: Advanced built-in functions (enmerate, eval, felter, zip)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43264177/article/details/102755966

3.pyhotn senior built-in functions
(1) enmerate
return an object can be enumerated, enmerate which make up an index, it can be obtained simultaneously by using the index value, and
(2) the eval
. 1) remove the contents of the string
2) character str string as a valid expression refers to find and return the results

# 取出字符串中的值
str = "[1,2,3,4]"
eval(str)
[1,2,3,4]
# 计算结果
>>>x=7
>>>eval('3 * x')
21

(3) filter filters

filert(参数1,参数2)

参数1:过滤规则的函数
参数2:要过滤的数据

Applications: Filter List Please li = [1,2,3,4,5,6,7,8,9,10] value greater than 5

(4) zip: polymerizing packaged
Here Insert Picture Description Applications:

# 现有以下数据
user_title = ["name", "age", "gender"]
user_info = [['小明', 18, '男'],
             ['小李', 19, '男'],
             ['小美', 17, '女']]

# 要求:将上述数据转换为以下格式(字典格式数据)
users = [{'name': '小明', 'age': '18', 'gender': '男'},
        {'name': '小李', 'age': '19', 'gender': '男'},
        {'name': '小美', 'age': '17', 'gender': '女'}]
# @time:2019/10/26 11:35
# @Author:coco
# @File:06高级内置函数.py
# @software:PyCharm

# enumerate的使用
li = [11, 22, 33, 44]

res = enumerate(li)

list2 = list(res)
print('----------list2--------')
print(list2)

# dic={"a":11,"b":22}
# print(list(dic.items()))

# eval:识别字符串中的python表达式
s1 = "(1,2,3)"
s2 = "[11,22,33]"
print('----------s1--------')
print(s1, type(s1))

# 识别字符串的元组
res1 = eval(s1)
print('----------res1--------')
print(res1, type(res1))

# 识别字符串的列表
res2 = eval(s2)
print('----------res2--------')
print(res2, type(res2))

# 注意点:如果是个纯粹的字符串,那么使用eval进行转换后就变成一个变量名
# 这样打印会报错,如果是一个纯粹的字符串去打印会报错,如果前面定义一个python,就不会报错
python = 666  # 如果没有定义python=666,打印的时候会报错
s3 = "python"
res3 = eval(s3)
print('----------res3--------')
print(res3, type(res3))

# 过滤函数:filter(参数1,参数2)
# 参数1:函数
# 参数2:待过滤的数据

# 案例 li=[11,22,33,44,1,2,3,4,5,77,2,323,90]

# 过滤大于33的数据

li = [11, 22, 33, 44, 1, 2, 3, 4, 5, 77, 2, 323, 90]


def func(x):
    return x > 33


print('----------new_list--------')
# 方式一
new_list = []
for i in li:
    if func(i):
        new_list.append(i)
print(new_list)

# 方式二:filter会自动过滤后面的列表,把列表的第一个元素当成参数传到前面的固定规则函数里面,
# 根据结果是返回true还是false,接着第二个元素,如果是true会根据数据把结果保留下来,如果是false,数据扔掉
# 依次类推,遍历完所有的元素
print('----------list1--------')
new_list1 = filter(func, li)
print(list(new_list1))

# 扩展知识点:
# 匿名函数:冒号后面就是返回值,经常跟felter结合使用
print('----------new_list2--------')
new_list2 = filter(lambda x: x > 33, li)
print(list(new_list2))

# zip 聚合打包的函数
li1 = [11, 22, 33, 44]
li2 = [111, 222, 333, 444]

res4 = zip(li1, li2)
print('----------res4-----------')
print(list(res4))

print('----------案例题-----------')

# 案例:
# 现有以下数据
user_title = ["name", "age", "gender"]
user_info = [['小明', 18, '男'],
             ['小李', 19, '男'],
             ['小美', 17, '女']]

# 要求:将上述数据转换为以下格式(字典格式数据)
users = [{'name': '小明', 'age': '18', 'gender': '男'},
         {'name': '小李', 'age': '19', 'gender': '男'},
         {'name': '小美', 'age': '17', 'gender': '女'}]

new_users = []
for user in user_info:
    data = zip(user_title, user)
    new_users.append(dict(data))
print(new_users)

# t=['name','age','gender']
# s=['小明','18','男']
#
# res5=zip(t,s)
# print(list(res5))
# print(dict(res5))

operation result:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43264177/article/details/102755966