# 8_一些常用函数()-其他

## 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")))``````
————————
``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")))``````