8_一些常用函数

2022/8/5 6:24:01

本文主要是介绍8_一些常用函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

sorted方法
t = ["FishC", "Apple", "Book", "Banana", "Pen"]
# sorted方法只是比较首个字母的ASCALL值,如果第一个字母相同就比较第二个字母
# ['Apple', 'Banana', 'Book', 'FishC', 'Pen']
s = sorted(t)
print(s)
# sorted(s,key=len)方法比较字母的长度,按照字母的长度排序
# ['Pen', 'Book', 'Apple', 'FishC', 'Banana']
print(sorted(s, key=len))
zip函数,zip_longest函数
x = [1, 2, 3]
y = [4, 5, 6]
z = "lover"
zipped = zip(x, y, z)
# [(1, 4, 'l'), (2, 5, 'o'), (3, 6, 'v')]
print(list(zipped))

import itertools
zipped = itertools.zip_longest(x, y, z)
# [(1, 4, 'l'), (2, 5, 'o'), (3, 6, 'v'), (None, None, 'e'), (None, None, 'r')]
print(list(zipped))
map函数
# map函数是包涵结果的迭代器
mapped = map(pow, [2, 3, 10], [5, 2, 3])
# [32, 9, 1000]
# 等价于[pow(2,5) pow(3,2) pow(10,3)]
print(list(mapped))

# filter函数返回的是计算结果为真的元素构成的迭代器,过滤器(str.islower)待过滤的迭代对象:"LoveR"
# 可迭代对象可以重复使用,迭代器只能使用一次
# ['o', 'v', 'e']
print(list(filter(str.islower, "LoveR")))

 



这篇关于8_一些常用函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程