预备知识:
reduce()
'''
用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算
'''
from functools import reduce
def add(x, y) :
return x + y
sum1 = reduce(add, [1,2,3,4,5])
sum2 = reduce(lambda x, y: x+y, [1,2,3,4,5])
print(sum1) # 15
print(sum2) # 15
enumerate
'''
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中
'''
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
'''
0 one
1 two
2 three
'''
format
'''
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能
基本语法是通过 {} 和 : 来代替以前的 %
'''
"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
hello world
"{0} {1}".format("hello", "world") # 设置指定位置
hello world
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
网站名:菜鸟教程, 地址 www.runoob.com
如果出现 : ,代表格式化数字
Original: https://www.cnblogs.com/daxiangcai/p/16895218.html
Author: 大湘菜
Title: chess video
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/713455/
转载文章受原作者版权保护。转载请注明原作者出处!