Python中号称神仙的六个内置函数详解

  • Post category:Python

Python中号称神仙的六个内置函数详解

在Python中,有许多强大的内置函数,其中有六个被认为是神仙级别的函数,它们分别是:map、reduce、filter、zip、sorted和enumerate。这些函数在Python中应用广泛,是每一个Python程序员都应该熟练掌握的技能之一。

map函数

map函数语法为:map(function, iterable, …)

map函数的作用是将一个或多个可迭代对象的每个元素应用于所提供的函数,然后返回一个包含结果的列表。

示例1:将列表中的每个数字平方

nums = [1, 2, 3, 4, 5]

def square(x):
    return x ** 2

squared_nums = list(map(square, nums))
print(squared_nums)
# 输出:[1, 4, 9, 16, 25]

示例2:将两个列表中的元素对应求和

list1 = [1, 2, 3]
list2 = [4, 5, 6]

sum_list = list(map(lambda x, y: x + y, list1, list2))
print(sum_list)
# 输出:[5, 7, 9]

reduce函数

reduce函数语法为:reduce(function, sequence, initial=None)

reduce函数的作用是将一个序列的前两个元素传递给所提供的函数,然后它将返回的结果跟第三个元素进行计算,依此类推,直到处理完整个序列,并返回最终结果。

示例1:求一个列表中所有数字的乘积

from functools import reduce

nums = [1, 2, 3, 4, 5]

def multiply(x, y):
    return x * y

result = reduce(multiply, nums)
print(result)
# 输出:120

示例2:将一个字符串列表合并为一个字符串

from functools import reduce

words = ["Hello", "World", "!"]

def concatenate(x, y):
    return x + y

result = reduce(concatenate, words)
print(result)
# 输出:HelloWorld!

filter函数

filter函数语法为:filter(function, iterable)

filter函数的作用是从可迭代对象中过滤出满足所提供函数的所有元素,并返回一个由这些元素组成的列表。

示例1:筛选出列表中的所有奇数

nums = [1, 2, 3, 4, 5]

def is_odd(x):
    return x % 2 == 1

odd_nums = list(filter(is_odd, nums))
print(odd_nums)
# 输出:[1, 3, 5]

示例2:筛选出一个字符串列表中所有长度大于等于5的字符串

words = ["Hello", "World", "Python", "Programming"]

def is_long_word(word):
    return len(word) >= 5

long_words = list(filter(is_long_word, words))
print(long_words)
# 输出:['Hello', 'World', 'Python', 'Programming']

zip函数

zip函数语法为:zip(*iterables)

zip函数的作用是将一个或多个可迭代对象的元素配对,然后返回一个由这些元素组成的元组列表。

示例1:将两个列表中的元素配对

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']

paired_list = list(zip(list1, list2))
print(paired_list)
# 输出:[(1, 'a'), (2, 'b'), (3, 'c')]

示例2:将多个列表中的元素配对

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = ['Amy', 'Bob', 'Cathy']

paired_list = list(zip(list1, list2, list3))
print(paired_list)
# 输出:[(1, 'a', 'Amy'), (2, 'b', 'Bob'), (3, 'c', 'Cathy')]

sorted函数

sorted函数语法为:sorted(iterable, key=None, reverse=False)

sorted函数的作用是对可迭代对象中的元素进行排序,并返回一个新的列表。

示例1:对一个列表中的数字进行升序排序

nums = [3, 2, 1, 5, 4]

sorted_nums = sorted(nums)
print(sorted_nums)
# 输出:[1, 2, 3, 4, 5]

示例2:对一个字符串列表按照字符串长度进行降序排序

words = ["Python", "Programming", "is", "awesome"]

sorted_words = sorted(words, key=lambda x: len(x), reverse=True)
print(sorted_words)
# 输出:['Programming', 'awesome', 'Python', 'is']

enumerate函数

enumerate函数语法为:enumerate(iterable, start=0)

enumerate函数的作用是生成一个枚举对象,包含可迭代对象中每个元素的索引编号和元素值,常用于for循环遍历时获取当前元素的索引。

示例1:遍历一个列表,并输出每个元素的索引和值

words = ["Hello", "World", "Python", "Programming"]

for i, word in enumerate(words):
    print(i, word)
# 输出:
# 0 Hello
# 1 World
# 2 Python
# 3 Programming

示例2:将一个字符串列表转换为一个字典,键为索引,值为字符串

words = ["Hello", "World", "Python", "Programming"]

word_dict = dict(enumerate(words))
print(word_dict)
# 输出:{0: 'Hello', 1: 'World', 2: 'Python', 3: 'Programming'}

以上便是Python中六个神仙函数的详细讲解,对于Python程序员来说,熟练掌握这些内置函数是必不可少的技能之一,希望本文可以帮助你更好地理解和使用它们。