def mul_table(number):
if number in range(1, 10):
for i in range(1,number+1):
for j in range(1, i+1):
print(i, "", j, "=", ij, end="\t")
print()
else:
print("The number is illegal")
mul_table()
格式: sort(self, /, , key=None, reverse=False)
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values. key: 是指定你排序规则的函数
格式: map(fun, iterrable) -> map object 作用: Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
(创建一个迭代器,使用每个可迭代对象的实参计算函数。当最短的可迭代对象耗尽时停止。)
list_data_1 = [1, 2]
list_data_2 = [3, 4, 5]
list_data_3 = [6, 7, 8, 9]
def function_test(i, j, x):
return i + 1, j + 1, x + 1
第一种用法
for i in map(function_test, list_data_1, list_data_2, list_data_3):
print(i) #将每个列表相同下标的元素以元组形式打包
第二种用法
list_data = list(map(function_test, list_data_1, list_data_2, list_data_3))
print(list_data)
格式: filter(function or None, iterable) --> filter object 作用: Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
(返回一个迭代器,生成对于某一个函数为可迭代的项为真值。如果function为None,则返回为真值的项。)
data = list(filter(lambda x : x % 2, [1, 2, 3, 0]))
print(data) #定义一个lambda函数,按照取余结果返回所有的真值
输出结果为: [1, 3]3. reudce
迁移到functools这个模块中 格式: def reduce(function, sequence, initial=_initial_missing):作用: Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value.
(将一个有两个参数的函数从左到右累加到序列的项上,以便将序列简化为单个值。)ruduce作用练习
from functools import reduce
data = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
print(data)