enumerate
>>> a=[1,2,3,4]
>>> for i,j in enumerate(a):
... print(i,j)
...
0 1
1 2
2 3
3 4
>>>
>
sort 函数
>>> list1 = [[0, 4, 1, 5], [3, 1, 5], [4, 0, 1, 5]]
>>> print(sorted(list1, key=lambda l: (len(l), l)))
[[3, 1, 5], [0, 4, 1, 5], [4, 0, 1, 5]]
>>> list2 = [[4, 5, 2], [2, 5, 4], [2, 4, 5]]
>>> print(sorted(list2, key=lambda l: (len(l), l)))
[[2, 4, 5], [2, 5, 4], [4, 5, 2]]
f string函数
>>> name = 'Fred'
>>> age = 42
>>> f'He said his name is {name} and he is {age} years old.'
He said his name is Fred and he is 42 years old.
它的效率比.format还高。链接f string
本文深入探讨了Python中enumerate函数的使用,展示了如何利用sort函数对列表进行高效排序,并介绍了fstring函数及其相较于.format的优势。通过具体实例,读者将学会更高效地处理数据和字符串。
411

被折叠的 条评论
为什么被折叠?



